// gbfuncs.js



function displayCountryOptions()
{
document.write('<select name="country" id="country" size="4" class="menuItems1">');
document.write('<option value="United States" selected>United States</option>'); 
document.write('<option value="Argentina">Argentina</option>');   
document.write('<option value="Australia">Australia</option>');  
document.write('<option value="Austria">Austria</option>');  
document.write('<option value="Brazil">Brazil</option>');  
document.write('<option value="Canada">Canada</option>'); 
document.write('<option value="Chile">Chile</option>');   
document.write('<option value="China">China</option>'); 
document.write('<option value="Denmark">Denmark</option>'); 
document.write('<option value="England">England</option>');   
document.write('<option value="Egypt">Egypt</option>');  
document.write('<option value="France">France</option>');  
document.write('<option value="Germany">Germany</option>');  
document.write('<option value="Great Britain">Great Britain</option>'); 
document.write('<option value="Iceland">Iceland</option>');   
document.write('<option value="India">India</option>'); 
document.write('<option value="Indonesia">Indonesia</option>');
document.write('<option value="Iran">Iran</option>');  
document.write('<option value="Ireland">Ireland</option>');   
document.write('<option value="Israel">Israel</option>');  
document.write('<option value="Italy">Italy</option>');   
document.write('<option value="Japan">Japan</option>'); 
document.write('<option value="Kuwait">Kuwait</option>');     
document.write('<option value="Mexico">Mexico</option>');    
document.write('<option value="Netherlands">Netherlands</option>');   
document.write('<option value="New Zealand">New Zealand</option>');
document.write('<option value="Norway">Norway</option>');
document.write('<option value="Nova Scotia">Nova Scotia</option>');   
document.write('<option value="Pakistan">Pakistan</option>'); 
document.write('<option value="Peru">Peru</option>');   
document.write('<option value="Russia">Russia</option>'); 
document.write('<option value="Scotland">Scotland</option>');
document.write('<option value="South Africa">South Africa</option>');  
document.write('<option value="South Korea">South Korea</option>'); 
document.write('<option value="Sweden">Sweden</option>');  
document.write('<option value="Switzerland">Switzerland</option>');
document.write('<option value="Thailand">Thailand</option>'); 
document.write('<option value="Wales">Wales</option>'); 
document.write('<option value="Zimbabwe">Zimbabwe</option>'); 
document.write('</select>');  
}


function displayShipCountryOptions()
{
document.write('<select name="shipcountry" id="shipcountry" size="4" class="menuItems1">');
document.write('<option value="United States" selected>United States</option>'); 
document.write('<option value="Argentina">Argentina</option>');   
document.write('<option value="Australia">Australia</option>');  
document.write('<option value="Austria">Austria</option>');  
document.write('<option value="Brazil">Brazil</option>');  
document.write('<option value="Canada">Canada</option>'); 
document.write('<option value="Chile">Chile</option>');   
document.write('<option value="China">China</option>'); 
document.write('<option value="Denmark">Denmark</option>'); 
document.write('<option value="England">England</option>');   
document.write('<option value="Egypt">Egypt</option>');  
document.write('<option value="France">France</option>');  
document.write('<option value="Germany">Germany</option>');  
document.write('<option value="Great Britain">Great Britain</option>'); 
document.write('<option value="Iceland">Iceland</option>');   
document.write('<option value="India">India</option>'); 
document.write('<option value="Indonesia">Indonesia</option>');
document.write('<option value="Iran">Iran</option>');  
document.write('<option value="Ireland">Ireland</option>');   
document.write('<option value="Israel">Israel</option>');  
document.write('<option value="Italy">Italy</option>');   
document.write('<option value="Japan">Japan</option>'); 
document.write('<option value="Kuwait">Kuwait</option>');     
document.write('<option value="Mexico">Mexico</option>');    
document.write('<option value="Netherlands">Netherlands</option>');   
document.write('<option value="New Zealand">New Zealand</option>');
document.write('<option value="Norway">Norway</option>');
document.write('<option value="Nova Scotia">Nova Scotia</option>');   
document.write('<option value="Pakistan">Pakistan</option>'); 
document.write('<option value="Peru">Peru</option>');   
document.write('<option value="Russia">Russia</option>'); 
document.write('<option value="Scotland">Scotland</option>');
document.write('<option value="South Africa">South Africa</option>');  
document.write('<option value="South Korea">South Korea</option>'); 
document.write('<option value="Sweden">Sweden</option>');  
document.write('<option value="Switzerland">Switzerland</option>');
document.write('<option value="Thailand">Thailand</option>'); 
document.write('<option value="Wales">Wales</option>'); 
document.write('<option value="Zimbabwe">Zimbabwe</option>'); 
document.write('</select>');  
}



function isvalidEmail(email)
    	{
    	var re = new RegExp();
    	re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
    	if (!re.test(email)) {return false;}
    	else {return true;}
}

// --------------------------------------------------------------
// Function scramble()
// Desc:    Disguise lowercase string (used for checking comment) 
//          (so user does not see the javascript)                                   
// Parms:   original string                           
// Return:  scrambled string
// --------------------------------------------------------------                        
function scramble(str)
{
	var sstr = "";
	var l;

	for (i=0; i<str.length; i++)
	{
		l = str.charAt(i); 
		switch (l)
        	{
        	case 'a':
            		sstr = sstr + 'L';
            		break;
        	case 'b':
            		sstr = sstr + 'h';
            		break;
                case 'c':
                    sstr = sstr + '2';
                    break;
                case 'd':
                    sstr = sstr + 'b';
                    break;
                case 'e':
                    sstr = sstr + 'Q';
                    break;
                case 'f':
                    sstr = sstr + '8';
                    break;
                case 'g':
                    sstr = sstr + 'N';
                    break;
                case 'h':
                    sstr = sstr + '6';
                    break;
                case 'i':
                    sstr = sstr + 'm';
                    break;
                case 'j':
                    sstr = sstr + 'p';
                    break;
                case 'k':
                    sstr = sstr + '1';
                    break;
                case 'l':
                    sstr = sstr + 'i';
                    break;
                case 'm':
                    sstr = sstr + '5';
                    break;
                case 'n':
                    sstr = sstr + 'd';
                    break;
                case 'o':
                    sstr = sstr + 'a';
                    break;
                case 'p':
                    sstr = sstr + 'j';
                    break;
                case 'q':
                    sstr = sstr + 'O';
                    break;
                case 'r':
                    sstr = sstr + 'e';
                    break;
                case 's':
                    sstr = sstr + 'k';
                    break;
                case 't':
                    sstr = sstr + 'f';
                    break;
                case 'u':
                    sstr = sstr + '3';
                    break;
                case 'v':
                    sstr = sstr + 'c';
                    break;
                case 'w':
                    sstr = sstr + 'g';
                    break;
                case 'x':
                    sstr = sstr + '4';
                    break;
                case 'y':
                    sstr = sstr + 'r';
                    break;
                case 'z':
                    sstr = sstr + '7';
                    break;
                case ' ':
                    sstr = sstr + 'z';
                    break;
                default:
                    sstr = sstr + '0';
		// end switch
                }
        // end for
	}

	return sstr;
}


// ---------------------------------------
// Function chk_language()
// Desc:    Check for inappropriate language    
// Parms:   comment string              
// Return:  true = comment has a problem
//          false = comment is fine
// --------------------------------------- 
function chk_language(str)
{
	
        var xstr = scramble(str);

	var re1 = /8+3+2+1+/i;
	var re2 = /k+6+m+f+/i;
	var re3 = /L+k+k+6+a+i+Q+/i;
	var re4 = /h+m+f+2+6+/i;
	var re5 = /h+L+k+f+L+e+b+/i;
	var re6 = /2+3+d+f+/i;
	var re7 = /g+6+a+e+Q+/i;
	var re8 = /d+L+7+m+/i;
	var re9 = /j+Q+d+m+k+/i;
	var re10 = /c+L+N+m+d+L+/i;
	var re11 = /zLkkz/i;
	var re12 = /zjmkk/i;
	var re13 = /L+k+k+g+6+a+i+Q+/i;
	var re13 = /L+k+k+g+6+a+i+Q+/i;
	var re14 = /z2eLj/i;
        var re15 = /p+L+2+1+L+k+k+/i;

        if (re1.test(xstr))
        		return true;
        if (re2.test(xstr))
        		return true;
        if (re3.test(xstr))
        		return true;
        if (re4.test(xstr))
        		return true;
        if (re5.test(xstr))
        		return true;
        if (re6.test(xstr))
        		return true;
        if (re7.test(xstr))
        		return true;
        if (re8.test(xstr))
        		return true;
        if (re9.test(xstr))
        		return true;
        if (re10.test(xstr))
        		return true;
        if (re11.test(xstr))
        		return true;
        if (re12.test(xstr))
        		return true;
        if (re13.test(xstr))
        		return true;
        if (re14.test(xstr))
        		return true;
        if (re15.test(xstr))
        		return true;

        return false;
}


// ---------------------------------------
// Function chk_embeddedHtml()
// Desc:    Check for certain embedded html in comment that causes
//          problems with subsequent display.    
// Parms:   comment string              
// Return:  true = comment has a problem
//          false = comment is fine
// --------------------------------------- 
function chk_embeddedHtml(str)
{
	
	var re1 = /href=/i;
	var re2 = /href\s+=/i;
	var re3 = /<a/i;

        if (re1.test(str))
        		return true;
        if (re2.test(str))
        		return true;
        if (re3.test(str))
        		return true;

        return false;
}


// ---------------------------------------
// Function validateZIP
// Desc:    Check for valid zip    
// Parms:   zip string              
// Return:  true = valid zip
//          false = not valid zip
// Comment: func courtesy of Javascript Source
// --------------------------------------- 
function validateZIP(field) {
var valid = "0123456789-";
var hyphencount = 0;

if (field.length!=5 && field.length!=10) {
//alert("Please enter your 5 digit or 5 digit+4 zip code.");
return false;
}
for (var i=0; i < field.length; i++) {
temp = "" + field.substring(i, i+1);
if (temp == "-") hyphencount++;
if (valid.indexOf(temp) == "-1") {
alert("Invalid characters in your zip code.  Please try again.");
return false;
}
if ((hyphencount > 1) || ((field.length==10) && ""+field.charAt(5)!="-")) {
alert("The hyphen character should be used with a properly formatted 5 digit+four zip code, like '12345-6789'.   Please try again.");
return false;
   }
}
return true;
}



// ---------------------------------------
// Function validatePhone
// Desc:    Check for valid phone    
// Parms:   phone string              
// Return:  true = valid phone
//          false = not valid phone
// --------------------------------------- 
/**
 * DHTML phone number validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */

// Declaring required variables
var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 10;

function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function checkInternationalPhone(strPhone){
s=stripCharsInBag(strPhone,validWorldPhoneChars);
return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}

function ValidatePhone(Phone){
	
	if (checkInternationalPhone(Phone)==false){
		return false
	}
	return true
 }


// ---------------------------------------
// Function IsNumeric(strString)
// Desc:    Check for numeric    
// --------------------------------------- 
function IsNumeric(strString)
   //  check for valid numeric strings	
   {
   var strValidChars = "0123456789";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   return blnResult;
   }





//  ---------------------------------------
// Function validate_form()
// Desc:    Validate the entry form    
// Parms:                
// Return:  true = go ahead and submit
//          false = do not submit
// --------------------------------------- 
function validate_form()
{


    // Get selection vals.
    $selst = document.forms[1].state.options[document.forms[1].state.selectedIndex].value;
    //$selshipst = document.document.forms[1].shipstate.options[document.document.forms[1].shipstate.selectedIndex].value;
    $selcntry = document.forms[1].country.options[document.forms[1].country.selectedIndex].value;
    //$selshipcntry = document.document.forms[1].shipcountry.options[document.document.forms[1].shipcountry.selectedIndex].value;

   
	// Ensure all required fields are entered.
	if (document.forms[1].fullname.value == "")
	{
		alert("Please enter your name.");
		document.forms[1].fullname.focus();
		return false;
	}

	if (document.forms[1].street1.value == "")
	{
		alert("Please enter your street.");
		document.forms[1].street1.focus();
		return false;
	}

	if (document.forms[1].city.value == "")
	{
		alert("Please enter your city.");
		document.forms[1].city.focus();
		return false;
	}


	if ($selcntry == "United States" && $selst == "") 
	{
		alert('For a US address, please enter a state.');
		document.forms[1].state.focus();
		return false;
	}

	if ($selcntry == "United States" && document.forms[1].zip.value == "") 
	{
		alert('For a US address, please enter a zip code.');
		document.forms[1].zip.focus();
		return false;
	}
/*
	if ($selcntry == "United States" && document.forms[1].province.value != "") 
	{
		alert('For a US address, please enter state and zip code, not province.');
		document.forms[1].province.focus();
		return false;
	}
*/
    if ($selcntry == "United States")
		if (validateZIP(document.forms[1].zip.value) == false)
		{
			alert("Please enter your correct 5 digit or 5 digit+4 zip code.");
			document.forms[1].zip.focus();
			return false;
		}
		
	if ($selcntry != "United States" && $selst != "") 
	{
		alert('For a non-US address, please do not select a US state.');
		document.forms[1].state.focus();
		return false;
	}
	
	if (document.forms[1].phone.value == "")
	{
		alert("Please enter a phone number.");
		document.forms[1].phone.focus();
		return false;
	}
	
	if (ValidatePhone(document.forms[1].phone.value) == false)
	{
		alert("Sorry, the phone number you entered is incorrect. (Remember to include area code.)");
		document.forms[1].phone.focus();
		return false;		
	}
	
	if (document.forms[1].email.value == "")
	{
		alert("Please enter an email address.");
		document.forms[1].email.focus();
		return false;
	}

        // Ensure email is ok.
	if (document.forms[1].email.value != "")
	{
		if (!isvalidEmail(document.forms[1].email.value))
		{
			alert("Sorry, the email you entered is not correct.");
			document.forms[1].email.focus();
			return false;
		}
    }

	
	if (document.forms[1].quantity.value == "" || document.forms[1].quantity.value == "0")
	{
		alert("Please enter the number of copies you want.");
		document.forms[1].quantity.focus();
		return false;
	}

	var is_num = IsNumeric(document.forms[1].quantity.value);
	if (is_num == false)
	{
		alert("Quantity must be a number.");
		document.forms[1].quantity.focus();
		return false;
	}
	
	// Ensure comment does not have unacceptable language or embedded html.
	var str = document.forms[1].comment.value;
	if (chk_language(str.toLowerCase())) 
	{
		alert('Your note contains inappropriate language. Please try again.');
		document.forms[1].comment.focus();
		return false;
	}



	return true;
}
