// *************************************************************
// file: global_styles.js
// description: JavaScript fuctions for the corporate Web sites.
// *************************************************************


var beenFocused = false;
document.onmousedown = markFocused;
function markFocused() {
	beenFocused = true;
}


function setFocusOnLoad() {
	if (!beenFocused) { loader(); }
}


// don't submit blank search request....
function isBlank()
{ 
	value=document.search_form.elements['sp-q'].value

	if ( value == "" )
	{ 
		alert("Please enter search word"); 
		return false; 
	} 
	else
	{ 
		return true; 
	} 
} 


/********
 * displays flash content or alternate
 * content is flash not detected. 
********/

function getSecureFlash(width, height, file, loop, alternate, classid)
{
	if (MM_FlashCanPlay)
	{
		var oeTags = '<OBJECT CLASSID="' + classid + '"'
         + 'WIDTH="' + width + '" HEIGHT="' + height + '"'
		 + 'CODEBASE="https://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab">'
         + '<PARAM NAME="MOVIE" VALUE="' + file + '">'
         + '<PARAM NAME="PLAY" VALUE="true">'
         + '<PARAM NAME="LOOP" VALUE="' + loop + '">'
         + '<PARAM NAME="QUALITY" VALUE="high">'
         + '<PARAM NAME="MENU" VALUE="false">'
         + '<EMBED SRC="' + file + '"'
         + 'WIDTH="' + width + '" HEIGHT="' + height + '"'
         + 'PLAY="true"'
         + 'LOOP="' + loop + '"'
         + 'QUALITY="high"'
         + 'MENU="false"'
         + 'TYPE="application/x-shockwave-flash"'
		 + 'PLUGINSPAGE="https://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash">'
         + '</EMBED>'
         + '</OBJECT>';
		document.write(oeTags);
	}
	else
	{
		var alternateContent = '<IMG SRC="' + alternate + '" HEIGHT="' + height + '" WIDTH="' + width + '" BORDER="0">'
		document.write(alternateContent);
	}
}

/********
 * Validate phone number for international countries
********/

function isValidPhoneNumber(num, requiredDigits) {
	//alert(num);
	//alert(requiredDigits);
    var digits = 0;
    if (num == null) return false;
    for( i=0; i<num.length; i++ ){
        var c = num.charCodeAt(i);  
        //convert the i-th character to ascii code value
        if( (c>=48) && (c<=57) ) digits++;
    }    
    return (digits >= requiredDigits);
}

/********
 * Verify that an email addres is valid
 * Written by Paolo Wales (paolo@taize.fr)
********/

function isValidEmail(emailad) {

   var exclude=/[^@\-\.\w]|^[_@\.\-]|[\._\-]{2}|[@\.]{2}|(@)[^@]*\1/;
   var check=/@[\w\-]+\./;
   var checkend=/\.[a-zA-Z]{2,4}$/;
   if(((emailad.search(exclude) != -1) ||
       (emailad.search(check)) == -1) ||
       (emailad.search(checkend) == -1)){
      return false;
   } else {
      return true;
   }
}

/********
 * Ensures valid emails are present and that the sender email doesn't contain
 * an 'officepartners.com' in the sender's domain.
 * 
 * use: for "email a friend" security
 * created by: Scott Yancey
********/

function isValidEmails() {
   
   var recipientEmail = document.forms[0].recipientEmail.value;
   var senderEmail = document.forms[0].senderEmail.value;
   var error = false;
   
   var errorMessage = "The following errors are present:\n\n";
   
   if (recipientEmail.indexOf(',') < 0) {
   		if (!isValidEmail(recipientEmail)){
   			error = true;
			errorMessage += "The recipient's Email address is not valid\n"
   	  }	
   } else {
   
   	 var addresses = new Array();
	 addresses = recipientEmail.split(',');
	 var currentAddress;
	 var i;
	 for (var i=0; i < addresses.length; i++) {      
	
		if (!isValidEmail(trim(addresses[i]))){
   			error = true;
   	    }
		
	 }
	 if (i > 10) {
			error = true;
			errorMessage += "Only 10 addresses are allowed\n"	
	 } 
	 if (error) {
	 	errorMessage += "A recipient's Email address is not valid\n"	
	 } 
	 
   }

   if (!isValidEmail(senderEmail)) {
   	error = true;
	errorMessage += "The sender's Email address is not valid"
   }
   if (senderEmail.indexOf("officepartners.com") >= 0) {
   	error = true;
	errorMessage += "The sender's Email address can not contain 'officepartners.com'."
   }
   
   if (error) {
      alert(errorMessage);
      return false;
   } else {
      return true;
   }
}

/********
*   Removes leading and trailing spaces from the passed string. Also removes
*   consecutive spaces and replaces it with one space. If something besides
*   a string is passed in (null, custom object, etc.) then return the input.
* 
*   created by: Scott Yancey
********/
function trim(inputString) {
   
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   return retValue; // Return the trimmed string back to the user
} // Ends the "trim" function

/*
 * Handle Phone Number
*/

function IsNumeric(sText)
{
   var ValidChars = "0123456789-.()";
   var IsNumber=true;
   var Char;
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;  
   }

/*
 * Main Validation
*/

function validate()
{
    var errorMessage = "Please fix the following errors: \n\n";
    var errorFound = 0;
	
	if (document.form.UserFirstName.value.length == 0) {errorMessage += "Enter your First Name\n"; errorFound = 1; }
    if (document.form.UserLastName.value.length == 0) {errorMessage += "Enter your Last Name\n"; errorFound = 1; }
    if (document.form.CompanyName.value.length == 0) {errorMessage += "Enter your Company Name\n"; errorFound = 1; }
	if (document.form.UserPhone.value.length == 0) {errorMessage += "Enter a Valid Phone Number\n"; errorFound = 1; }
	if (!IsNumeric(document.form.UserPhone.value)) {errorMessage += "Please enter only numbers in the Phone Number\n"; errorFound = 1; } 
	if (!isValidEmail(document.form.UserEmail.value)) {errorMessage += "Enter a Valid Email Address\n"; errorFound = 1; }
	//if (isValidPhoneNumber(document.form.UserPhone.value,document.form.UserPhone.value.length)) {errorMessage += "Enter a Valid Phone Number\n"; errorFound = 1; }
	/*if (document.form.CompanyAddress.value.length == 0) {errorMessage += "Enter your Street Address\n"; errorFound = 1; }
    if (document.form.CompanyCity.value.length == 0) {errorMessage += "Enter your City\n"; errorFound = 1; }
	
	    isStateRequired = (document.form.CompanyCountry.value == '' || document.form.CompanyCountry.value == 'US' || document.form.CompanyCountry.value == 'CA');
	    if (isStateRequired) {
			if (document.form.elements['CompanyState'].selectedIndex == 0) {errorMessage += "Enter your State/Province\n"; errorFound = 1; }
	    }
    
	if (document.form.CompanyPostalCode.value.length == 0) {errorMessage += "Enter your Zip/Postal Code\n"; errorFound = 1; }
    if (document.form.elements['CompanyCountry'].selectedIndex == 0) {errorMessage += "Choose a Valid Country\n"; errorFound = 1; }
    if (document.form.elements['CompanyEmployees'].selectedIndex == 0) {errorMessage += "Select the Number of Employees\n"; errorFound = 1; }*/

    if (errorFound == 1)
	{
        alert(errorMessage);
        return false;
    }
	else
	{
		return true;
    }
}

