/*
File:  		JsUtilities.js
Description: All the JavaScript Utilities
*/
var oldDiv, banner;


// Pops up a child window with the given window name and dimensions.
// Takes the optional arguments to set the menubar, resize, and scrollbars attributes of Window.
function popupWindow(winname,  w, h, menu, resize, scroll) {
	if (winname == null) winname = "newWindow";
	if (w == null) w = 600;
	if (h == null) h = 600;
	if (resize == null) resize = 1;
	
	menutype   = "nomenubar";
	resizetype = "noresizable";
	scrolltype = "noscrollbars";
	if (menu) menutype = "menubar";
	if (resize) resizetype = "resizable";
	if (scroll) scrolltype = "scrollbars";
	
	cwin=window.open('', winname,
		"status," + menutype + "," + scrolltype + "," + resizetype + ",width=" + w + ",height=" + h);
return true;
}

function popupSign(winname,  w, h, menu, resize, scroll) {
	if (winname == null) winname = "newWindow";
	if (w == null) w = 600;
	if (h == null) h = 600;
	if (resize == null) resize = 1;
	
	menutype   = "nomenubar";
	resizetype = "noresizable";
	scrolltype = "noscrollbars";
	if (menu) menutype = "menubar";
	if (resize) resizetype = "resizable";
	if (scroll) scrolltype = "scrollbars";
	
	cwin=window.open('','newWindow','height=200,width=400,status=yes,toolbar=no,menubar=no,location=no');
return true;
}


function trim(string) {
   while (string.charAt(0) == " ")
      string = string.substring(1,string.length);

   while (string.charAt(string.length-1) == " ") 
      string = string.substring(0,string.length-1);

   return string;
}

function checkNumeric(element, whole, fractional) {

   if (fractional == 0) {
	 var value = parseInt(element.value);
   } else {
	 var value = parseFloat(element.value);
   }
//   if (trim(element.value) == "") return true;

   if (isNaN(value)) {
      alert("Please enter a number");
      element.focus();
      return false;
   }

   element.value = value;

   if (whole != null) {
      if (value >= Math.pow(10,whole)) {
         element.value = value;
         alert("Please enter a number less than " + Math.pow(10,whole));
         element.focus();
         return false;
      }

      if (fractional != null && fractional != 0) {
         temp = Math.floor((value-Math.floor(value)) * Math.pow(10,fractional)) 
         temp += Math.pow(10,fractional)
         temp += ""
         element.value = Math.floor(value) + "." + temp.substring(1,temp.length);
	  }
   }

   return true;
}

// A function to validate the E-mail
 function validEmail(email) {
    invalidChars = " /:,;"

  if (email == "") {      // cannot be empty
   return false
  }
  for (i=0; i<invalidChars.length; i++) { // does it contain any invalid characters?
   badChar = invalidChars.charAt(i)
   if (email.indexOf(badChar,0) > -1) {
    return false
   }
  }
  atPos = email.indexOf("@",1)   // there must be one "@" symbol
  if (atPos == -1) {
   return false
  }
  if (email.indexOf("@",atPos+1) != -1) { // and only one "@" symbol
   return false
  }
  periodPos = email.indexOf(".",atPos)
  if (periodPos == -1) {     // and at least one "." after the "@"
   return false
  }
  if (periodPos+3 > email.length) {  // must be at least 2 characters after the "."
   return false
  }
  return true
}

// Check form field content.
function checkField (thisform, fieldvalue) {
	if (thisform.BookmarkName.value == "") {
	   alert ('Please enter ' +  fieldvalue);
	   return false;
	}
	return true;
}

// Check form for E-mail addresses
 function checkForm (emailform) {
 	  if (emailform.email_target_email.value == "") {
	      alert ('Please enter "Send to" address.');
		  return false
	  }
	  else {  
		   if (!validEmail(emailform.email_target_email.value)) {
		    alert('Invalid "Send to" email address')
		    emailform.email_target_email.focus()
		    emailform.email_target_email.select()
		    return false
		   }
	  }
	  
	   if (emailform.email_from.value == "") {
	      alert ('Please enter "Your email" address.');
		  return false
	   }
	   else {
	   	   if (!validEmail(emailform.email_from.value)) {
		    alert('Invalid email address')
		    emailform.email_from.focus()
		    emailform.email_from.select()
		    return false
		   }
	   }

	  return true
 }

// Check thisform's multiple select list and give an error if selected 
// items count is more than maxcount.
function checkSelectionCount (thisform, selname, maxcount) {
   var j = 0;
   for (var i=0; i<thisform.elements[selname].options.length; i++) {
   	   if(thisform.elements[selname].options[i].selected) j++;
   }

   if (j > maxcount) {
	   alert("Please select " + maxcount + " or fewer items.\nCurrently selected items: " + j);
       return false;
   }
   else {
       return true;
   }

}

// Pops up a child window with the given window name and dimensions
/*
function popupWindow(winname,w,h) {
	if (winname == null) winname = "newWindow";
	if (w == null) w = 600;
	if (h == null) h = 600;
	cwin=window.open("", winname,
	"nomenubar,status,scrollbars,noresizable,width=" + w + ",height=" + h);
return true;
}
*/

// Submits the form associated with "thiswin" window and displays the results in 
// "popupname" window of dimensions 'w' and 'h'.  Uses popupWindow function. 
function submitClose(thiswin, popupname, w, h, menu, resize, scroll) {
	if (popupname == null) popupname = "resultswindow";
	if (w == null) w = 600;
	if (h == null) h = 600;
	popupWindow(popupname, w, h, menu, resize, scroll);
	
	thiswin.document.forms[0].submit();
	
	thiswin.close();
}

// Checks(status true) or unchecks(status false) all the checkboxes associated with the form.
function CheckUncheck(status) {
   for (i = 0; i < document.forms[0].length; i++) {
      document.forms[0].elements[i].checked = status
   }
}

// Handles switching on and off of the Design, Proof, and Order Online vid
function showHideFlash(fmDivSwitch) {
		var fmDiv = window.document.getElementById('plasma');
		
			if (fmDivSwitch == "on") {
				var wcenter = window.document.body.clientWidth / 2;
				var flashcode = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="900" height="700" id="FlashID" tabindex="27" title="Design, Proof & Order Online">';
                flashcode += '<param name="movie" value="http://www.deesign.com/flash/Plasma-TV.swf" />';
    			flashcode += '<param name="quality" value="high" />';
    			flashcode += '<param name="wmode" value="transparent" />';
    			flashcode += '<param name="swfversion" value="6.0.65.0" />';
				flashcode += '<param name="allowScriptAccess" value="always" />';
    			flashcode += '<param name="expressinstall" value="http://www.deesign.com/Scripts/expressInstall.swf" />';
    			flashcode += '<!-- Next object tag is for non-IE browsers. So hide it from IE using IECC. -->';
    			flashcode += '<!--[if !IE]>-->';
    			flashcode += '<object type="application/x-shockwave-flash" data="http://www.deesign.com/flash/Plasma-TV.swf" width="661" height="563">';
    			flashcode += '  <!--<![endif]-->';
    			flashcode += '  <param name="quality" value="high" />';
    			flashcode += '  <param name="wmode" value="transparent" />';
				flashcode += '<param name="allowScriptAccess" value="always" />';
    			flashcode += '  <param name="swfversion" value="6.0.65.0" />';
    			flashcode += '  <param name="expressinstall" value="http://www.deesign.com/Scripts/expressInstall.swf" />';
    			flashcode += '  <!-- The browser displays the following alternative content for users with Flash Player 6.0 and older. -->';
    			flashcode += '  <div>';
    			flashcode += '    <h4>Content on this page requires a newer version of Adobe Flash Player.</h4>';
    			flashcode += '    <p><a href="http://www.adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" width="112" height="33" /></a></p>';
    			flashcode += ' </div>';
    			flashcode += ' <!--[if !IE]>-->';
    			flashcode += ' </object>';
    			flashcode += '<!--<![endif]-->';
    			flashcode += '</object>';
				//alert(flashcode);
				
				
				fmDiv.innerHTML = '<div id="plasmaXY" style="position:relative; z-index:99; left:-250px; top: 0px; ">'+ flashcode +' </div>';
				
				//expressinstall_url = false; 
				//flashvars = { oldTitle: window.document.title };  
        		//params = { title: 'DeeSign', quality: 'high', wmode: 'transparent', allowScriptAccess: 'always', menu: 'false' }; 
		        //attributes = { id: 'plasmaXY' }; 
				//swfobject.embedSWF('flash/Plasma-TV.swf', 'plasmaXY', '900', '700', '8.0.0', expressinstall_url, flashvars, params, attributes);  //1200x900
			    
			} else {
				fmDiv.innerHTML = "";
				
			}
	}
	


	function blank2(d) {
		if (d == "Banner") document.getElementById("Dee_Banner").innerHTML = '<img src="Index_images2/'+banner+'" alt="The Sign that Shows You Made a Dream Come True" width="667" height="203" />'; 
		if (d == "Main") document.getElementById("Franchise_Content").innerHTML = '<br />'; 
		if (d == "Sub") document.getElementById("Franchise_Content").innerHTML = '<br />'; 
	}

	function insertflash2() {
		expressinstall_url = false; 
		params = { title: 'DeeSign', quality: 'high', wmode: 'transparent', allowScriptAccess: 'always', menu: 'false' };
		flashvars = { oldTitle: window.document.title };  
        attributes = { id: 'riderDemo' }; 
		swfobject.embedSWF('flash/riderDemo.swf', 'customriders', '400', '232', '8.0.0', expressinstall_url, flashvars, params, attributes);
		matchHeight();
	}
	
	function insertSubImg(d, img) {
		document.getElementById(d).innerHTML = '<img src="http://www.deesign.com/images/layout/'+img+'"  />';
		
	}

	function insertSubPage(d, c) {
		document.getElementById(d).innerHTML = document.getElementById(c).innerHTML;
		matchHeight();
	}
	
function matchHeight(){
 	var contentDiv =  document.getElementById('Franchise_Content').offsetHeight;
	var leftDiv =  document.getElementById('leftNav').offsetHeight;
	var rightDiv =  document.getElementById('rightNav').offsetHeight;
	document.getElementById('Franchise_Content').style.width = 667;
	
	if (contentDiv > leftDiv + 75 && contentDiv > rightDiv + 20) {
		document.getElementById('MainBody').style.height = contentDiv;
		document.getElementById('lSep').style.height = contentDiv;
		document.getElementById('leftNav').style.height = contentDiv;
		document.getElementById('rightNav').style.height = contentDiv;
		document.getElementById('rSep').style.height = contentDiv;
	}
	if (leftDiv > contentDiv - 75 && leftDiv > rightDiv - 55) {
		document.getElementById('MainBody').style.height = leftDiv + 75;
		document.getElementById('Franchise_Content').style.height = leftDiv + 75;
		document.getElementById('rSep').style.height = leftDiv + 75;
		document.getElementById('rightNav').style.height = leftDiv + 75;
		document.getElementById('leftNav').style.height = leftDiv + 75;
		document.getElementById('lSep').style.height = leftDiv + 75;
	}
	
	if (rightDiv > contentDiv - 20 && rightDiv > leftDiv + 55) {
		document.getElementById('MainBody').style.height = rightDiv +20;
		document.getElementById('Franchise_Content').style.height = rightDiv+20;
		document.getElementById('rSep').style.height =  rightDiv+20;
		document.getElementById('rightNav').style.height = rightDiv+20;
		document.getElementById('leftNav').style.height = rightDiv+20;
		document.getElementById('lSep').style.height = rightDiv+20;
	}
}

function matchHeight2(){
 	var contentDiv =  document.getElementById('Franchise_Content').offsetHeight;
	var leftDiv =  document.getElementById('leftNav').offsetHeight;
	document.getElementById('Franchise_Content').style.width = 870;
	
	if (contentDiv > leftDiv + 75) {
		document.getElementById('MainBody').style.height = contentDiv;
		document.getElementById('lSep').style.height = contentDiv;
		document.getElementById('leftNav').style.height = contentDiv;
	
	}
	if (leftDiv > contentDiv - 75) {
		document.getElementById('MainBody').style.height = leftDiv + 75;
		document.getElementById('Franchise_Content').style.height = leftDiv + 75;
		document.getElementById('leftNav').style.height = leftDiv + 75;
		document.getElementById('lSep').style.height = leftDiv + 75;
	}
	
	
}

function matchHeightImg(s){
 	var contentDiv = s;
	var leftDiv =  document.getElementById('leftNav').offsetHeight;
	var rightDiv =  document.getElementById('rightNav').offsetHeight;

	if (contentDiv > leftDiv + 75 && contentDiv > rightDiv + 20) {
		document.getElementById('MainBody').style.height = contentDiv;
		document.getElementById('leftNav').style.height = contentDiv;
		document.getElementById('rightNav').style.height = contentDiv;
	}
	if (leftDiv > contentDiv - 75 && leftDiv > rightDiv - 55) {
		document.getElementById('MainBody').style.height = leftDiv + 75;
		document.getElementById('Franchise_Content').style.height = leftDiv + 75;
		document.getElementById('rightNav').style.height = leftDiv + 75;
		document.getElementById('leftNav').style.height = leftDiv + 75;
	}
	
	if (rightDiv > contentDiv - 20 && rightDiv > leftDiv + 55) {
		document.getElementById('MainBody').style.height = rightDiv +20;
		document.getElementById('Franchise_Content').style.height = rightDiv+20;
		document.getElementById('rightNav').style.height = rightDiv+20;
		document.getElementById('leftNav').style.height = rightDiv+20;
	}
}