window.onload = initPage;
window.onresize = initPage;


/***********************************************
* Bookmark site script- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
***********************************************/
/* Modified to support Opera */
/*  http://www.dynamicdrive.com/dynamicindex9/addbook.htm */
function bookmarksite(title,url) {
    if (window.sidebar) { // firefox
        window.sidebar.addPanel(title, url, "");
    } else if(window.opera && window.print) { // opera
        var elem = document.createElement('a');
        elem.setAttribute('href',url);
        elem.setAttribute('title',title);
        elem.setAttribute('rel', 'sidebar');
        elem.click();
    } else if(document.all) { // ie
        window.external.AddFavorite(url, title);
    }
}
    
    
// 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 initPage() {
    var masterContainerObj = document.getElementById("masterContainer");
    
    if (typeof(window.innerWidth) == 'number') {
        //Non-IE
        w = window.innerWidth;
        h = window.innerHeight;
    } else if (document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight)) {
        //IE 6+ in 'standards compliant mode'
        w = document.documentElement.clientWidth;
        h = document.documentElement.clientHeight;
    } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
        //IE 4 compatible
        w = document.body.clientWidth;
        h = document.body.clientHeight;
    }
    masterContainerObj.style.height = h - 40;
}

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 trim(s)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not a whitespace, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (c != " ") returnString += c;
    }
    return returnString;
}
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 checkSearch(frm, key) {
    var frmObj = document.getElementById(frm);
    var keyObj = document.getElementById(key);
    
    if(trim(keyObj.value) == '') {
        keyObj.style.border = '1px solid red';
        keyObj.focus();
    } else {
        frmObj.submit();
    }
}

function checkInternationalPhone(strPhone){
    var bracket=3;
    
    strPhone=trim(strPhone);
    if(strPhone.indexOf("+")>1)
        return false;
    if(strPhone.indexOf("-")!=-1)
        bracket=bracket+1;
    if(strPhone.indexOf("(")!=-1 && strPhone.indexOf("(")>bracket)
        return false;
    
    var brchr=strPhone.indexOf("(");
    
    if(strPhone.indexOf("(")!=-1 && strPhone.charAt(brchr+2)!=")")
        return false;
    if(strPhone.indexOf("(")==-1 && strPhone.indexOf(")")!=-1)
        return false;
    s=stripCharsInBag(strPhone,validWorldPhoneChars);
    
    return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}

function openProducts(pcat) {
    var pcatObj = document.getElementById('p' + pcat);
    
    switch(pcatObj.style.overflow) {
        case 'visible':
            pcatObj.style.overflow = 'hidden';
            pcatObj.style.visibility = 'hidden';
            pcatObj.style.height = '1px';
            break;
        case 'hidden':
            pcatObj.style.overflow = 'visible';
            pcatObj.style.visibility = 'visible';
            pcatObj.style.height = '';
            break;
    }
}

function emailCheck (emailStr) {
    /* The following pattern is used to check if the entered e-mail address
       fits the user@domain format.  It also is used to separate the username
       from the domain. */
    var emailPat=/^(.+)@(.+)$/
    /* The following string represents the pattern for matching all special
       characters.  We don't want to allow special characters in the address. 
       These characters include ( ) < @ , ; : \ " . [ ]    */
    var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
    /* The following string represents the range of characters allowed in a 
       username or domainname.  It really states which chars aren't allowed. */
    var validChars="\[^\\s" + specialChars + "\]"
    /* The following pattern applies if the "user" is a quoted string (in
       which case, there are no rules about which characters are allowed
       and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
       is a legal e-mail address. */
    var quotedUser="(\"[^\"]*\")"
    /* The following pattern applies for domains that are IP addresses,
       rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
       e-mail address. NOTE: The square brackets are required. */
    var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
    /* The following string represents an atom (basically a series of
       non-special characters.) */
    var atom=validChars + '+'
    /* The following string represents one word in the typical username.
       For example, in john.doe@somewhere.com, john and doe are words.
       Basically, a word is either an atom or quoted string. */
    var word="(" + atom + "|" + quotedUser + ")"
    // The following pattern describes the structure of the user
    var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
    /* The following pattern describes the structure of a normal symbolic
       domain, as opposed to ipDomainPat, shown above. */
    var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
    
    
    /* Finally, let's start trying to figure out if the supplied address is
       valid. */
    
    /* Begin with the coarse pattern to simply break up user@domain into
       different pieces that are easy to analyze. */
    var matchArray=emailStr.match(emailPat)
    if (matchArray==null) {
      /* Too many/few @'s or something; basically, this address doesn't
         even fit the general mould of a valid e-mail address. */
    	//alert("Email address seems incorrect (check @ and .'s)")
    	return false
    }
    var user=matchArray[1]
    var domain=matchArray[2]
    
    // See if "user" is valid 
    if (user.match(userPat)==null) {
        // user is not valid
        //alert("The part of your email address before the '@' doesn't seem to be valid.")
        return false
    }
    
    /* if the e-mail address is at an IP address (as opposed to a symbolic
       host name) make sure the IP address is valid. */
    var IPArray=domain.match(ipDomainPat)
    if (IPArray!=null) {
        // this is an IP address
    	  for (var i=1;i<=4;i++) {
    	    if (IPArray[i]>255) {
    	        //alert("Destination IP address is invalid!")
    		return false
    	    }
        }
        return true
    }
    
    // Domain is symbolic name
    var domainArray=domain.match(domainPat)
    if (domainArray==null) {
    	//alert("Part of your email address after the '@' doesn't seem to be valid")
        return false
    }
    
    /* domain name seems valid, but now make sure that it ends in a
       three-letter word (like com, edu, gov) or a two-letter word,
       representing country (uk, nl), and that there's a hostname preceding 
       the domain or country. */
    
    /* Now we need to break up the domain to get a count of how many atoms
       it consists of. */
    var atomPat=new RegExp(atom,"g")
    var domArr=domain.match(atomPat)
    var len=domArr.length
    if (domArr[domArr.length-1].length<2 || 
        domArr[domArr.length-1].length>6) {
       // the address must end in a two letter or other TLD including museum
       //alert("The address must end in a top level domain (e.g. .com), or two letter country.")
       return false
    }
    
    // Make sure there's a host name preceding the domain.
    if (len<2) {
       var errStr="This address is missing a hostname!"
       //alert(errStr)
       return false
    }
    
    // If we've got this far, everything's valid!
    return true;
}

function strTrim(stringa) {
    while (stringa.substring(0,1) == ' ') {
        stringa = stringa.substring(1, stringa.length);    
    }
    while (stringa.substring(stringa.length-1, stringa.length) == ' ') {
        stringa = stringa.substring(0,stringa.length-1);
    }
    return stringa;
}

function checkOrder(msg) {
    var nameObj = document.getElementById('order[name]');
    var companyObj = document.getElementById('order[company]');
    var emailObj = document.getElementById('order[email]');
    var phoneObj = document.getElementById('order[phone]');
    var addressObj = document.getElementById('order[address]');
    var cityObj = document.getElementById('order[city]');
    var countryObj = document.getElementById('order[country]');
    var zipObj = document.getElementById('order[zip]');
    var rndcheckObj = document.getElementById('rndcheck');
    var rndinputObj = document.getElementById('rndinput');
    
    nameObj.style.border = '1px solid #000000';
    companyObj.style.border = '1px solid #000000';
    emailObj.style.border = '1px solid #000000';
    phoneObj.style.border = '1px solid #000000';
    addressObj.style.border = '1px solid #000000';
    cityObj.style.border = '1px solid #000000';
    countryObj.style.border = '1px solid #000000';
    zipObj.style.border = '1px solid #000000';
    rndinputObj.style.border = '1px solid #000000';
    
    nameObj.value = strTrim(nameObj.value);
    companyObj.value = strTrim(companyObj.value);
    emailObj.value = strTrim(emailObj.value);
    phoneObj.value = strTrim(phoneObj.value);
    addressObj.value = strTrim(addressObj.value);
    cityObj.value = strTrim(cityObj.value);
    countryObj.value = strTrim(countryObj.value);
    zipObj.value = strTrim(zipObj.value);
    
    if (nameObj.value == '') {
        nameObj.style.border = '1px solid red';
        nameObj.focus();
        scroll(0,0);
        return(false);
    }
    if (companyObj.value == '') {
        companyObj.style.border = '1px solid red';
        companyObj.focus();
        scroll(0,0);
        return(false);
    }
    if (emailObj.value == '' && phoneObj.value == '') {
        emailObj.style.border = '1px solid red';
        phoneObj.style.border = '1px solid red';
        emailObj.focus();
        scroll(0,0);
        return(false);
    }
    if (emailObj.value != '' && !emailCheck(emailObj.value)) {
        emailObj.style.border = '1px solid red';
        emailObj.focus();
        scroll(0,0);
        return(false);
    }
    if (phoneObj.value != '' && !checkInternationalPhone(phoneObj.value)) {
        phoneObj.style.border = '1px solid red';
        phoneObj.focus();
        scroll(0,0);
        return(false);
    }
    if (addressObj.value == '') {
        addressObj.style.border = '1px solid red';
        addressObj.focus();
        scroll(0,0);
        return(false);
    }
    if (cityObj.value == '') {
        cityObj.style.border = '1px solid red';
        cityObj.focus();
        scroll(0,0);
        return(false);
    }
    if (countryObj.value == '') {
        countryObj.style.border = '1px solid red';
        countryObj.focus();
        scroll(0,0);
        return(false);
    }
    if (zipObj.value == '') {
        zipObj.style.border = '1px solid red';
        zipObj.focus();
        scroll(0,0);
        return(false);
    }
    if (rndinputObj.value != rndcheckObj.value) {
        rndinputObj.style.border = '1px solid red';
        rndinputObj.focus();
        scroll(0,0);
        return(false);
    }
    
    return(true);
}