///////////////////////////////////////////////////////////////////////////////
//Validation of address book                                                 //
///////////////////////////////////////////////////////////////////////////////


//globals
var nsResultStr;
var netscapeGtg = 0;


//this guy tells what browser i'm using
function WhatBrowser() {
    var browser; 
    if (navigator.userAgent.indexOf("Win") != -1) {
        if (navigator.userAgent.indexOf("MSIE") != -1) {
            browser = "iewin";
        } else {
            browser = "netwin";
        }
    } else {
         if (navigator.userAgent.indexOf("MSIE") != -1) {
             browser = "iemac";
         } else {
             browser = "netmac";
         }
    }
    return browser;
}


///////////////////////////////////////////////////////////////////////////////
/// These are some util functions they are common throughout all the         //
//  validation scripts. TODO: I might try and put these in common.js         //
///////////////////////////////////////////////////////////////////////////////

/* fake struct type thingy
 *
 *
 */
 function resultTable(result,name,type) {
 	this.result = result
 	this.name = name
	this.type = type
 }
 

/* is Num - this checks to see if a string is only numbers. if the function finds a char that
 *          is a number it returns false. It takes a str argument.
 *
 */
function isNum(str) {
    var i;
    for(i=0;i<str.length;i++) {
        if(isNaN(str.charAt(i))) {
            return Boolean(false);
        }
    }
    return Boolean(true);
}


/* isChar -  this just checks to see if the str has any numbers in it.  If the function
 *           finds a number it returns false. It takes a str argument. TODO: ignores
 *			 white space.
 */
function isChar(str) {
	var i;
    for(i=0;i<str.length;i++){
        if(isNaN(str.charAt(i))) {
        	return Boolean(true);
        } 
        return Boolean(false);
    }
	return Boolean(false);
}


/* isValidZip - this is wrapper function which uses isNum().  It takes str argument.
 *				if the function finds an invalid zip it returns false. It takes a str argument.
 *
 */
function isValidZip(str) {
   var i,count;
	count = 0;
	for(i=0;i<str.length;i++) {
		if(!isNaN(str.charAt(i))) {
			count++;
		}
	}
	//just counting digits right now.  
	if(count == 5) {
		return Boolean(true);
	}
	else {
		return Boolean(false);
	}
}


/* isValidPhoneNumber - this function checks to see if phone number is valid.
 *						It takes a str argument.
 *
 */
function isValidPhoneNumber(str) {
	var i,count;
	count = 0;
	for(i=0;i<str.length;i++) {
		if(!isNaN(str.charAt(i))) {
			count++;
		}
	}
	//just counting digits right now.  
	if(count == 10) {
		return Boolean(true);
	}
	else {
		return Boolean(false);
	}
}

/*  isValidElmail - this function check to see if an email is valid. It takes a str 
argument.
 *
 *
 */
function isValidEmail(str) {
    var i,j;
    i=0;
    j=0;
    i = str.indexOf("@");
    
    if(i < 0) {
        return Boolean(false);
    }
    
    if(str.indexOf(".",i) > 0){
        return Boolean(true);
    }
}

/*	isBlank - this function checks to see if the text or password field is blank
 *
 *
 */
function isBlank(str) {
	if(str == "") {
		return Boolean(true);
	}
	return Boolean(false);
}


/*	isSelected - this function checks to see if the text or password field is blank
 *               TODO: might need to make this more general
 *
 */
function isSelected(str) {
	if(str == "none") {
		return Boolean(false);
	}
	return Boolean(true);
}

function isValidPasswords(str1,str2) {
    if(str1 == "" || str2 == "") {
        return Boolean(false);
    }
    if(str1 != str2){
	//alert('Your passwords do not match');
        return Boolean(false)
    }
    return Boolean(true);
}

function isValidCity(str) {
    return Boolean((!isBlank(str)) && (isChar(str)));
}

function isValidState(str) {
    if(str=="Choose") {
       return Boolean(false);
    }
    return Boolean(true);
}

function isSameAsBillingChecked(form) {
    
    return Boolean(form.same_as_billing.checked);
}

function IEValidateText(str){
    var e = eval(str);
    e.className = "valid";
}

function IEInValidateText(str) {
    var e = eval(str);
    e.className = "invalid";
}

//if isSameAsBilling not checked return false
function ReturnStringForSameAsBillingInputs(formInputName,form) {
    var e = eval("s_"+formInputName.substr(0,2));
    //alert(e);
}


//this function combines various validate routines into one
function doCrossBrowserValidation(currentResultTable,browser){
    
    //ignore platform just interested in what browser
    if(browser.indexOf("ie") > -1) {
	if(currentResultTable.result){
            IEValidateText(currentResultTable.name);
	    return Boolean(true);
        }
        else {
	    IEInValidateText(currentResultTable.name);
	    return Boolean(false);
        } 
    }
    else if(browser.indexOf("net") > -1) {
        if(currentResultTable.result){
            return Boolean(true);
	}
	else {
	    return Boolean(false);
        }
    }
}



var gtg;

function ValidateAddress(form) {

    var i,browser,res;
    var resultsTable = new Array();
    gtg = 0;    

    browser = WhatBrowser();

    for(i=0;i<form.elements.length;i++){
      
        if((form.elements[i].name.indexOf("first") > -1)
           || (form.elements[i].name.indexOf("last") > -1)
           //|| (form.elements[i].name.indexOf("company") > -1)
	   || (form.elements[i].name.indexOf("address") > -1 && form.elements[i].name.indexOf("address2") == -1)
	   || (form.elements[i].name.indexOf("city") > -1)) {
               
            resultsTable[i] = new resultTable(!isBlank(form.elements[i].value),
                                             form.elements[i].name,
                                             form.elements[i].type);

            res = doCrossBrowserValidation(resultsTable[i],browser);  
            if(!res){
	        gtg++;
            }  
	}
        if(form.elements[i].name.indexOf("state") > -1) {

            resultsTable[i] = new resultTable(isValidState(form.elements[i].value),
                                             form.elements[i].name,
                                             form.elements[i].type);

	    res = doCrossBrowserValidation(resultsTable[i],browser);
	    if(!res){
	        gtg++;
            } 
	}
        if(form.elements[i].name.indexOf("zip") > -1) {
               
            resultsTable[i] = new resultTable(isValidZip(form.elements[i].value),
                                             form.elements[i].name,
                                             form.elements[i].type);

            res = doCrossBrowserValidation(resultsTable[i],browser);
            if(!res){
	        gtg++;
            }   
	}
        if(form.elements[i].name.indexOf("email") > -1) {

            resultsTable[i] = new resultTable(isValidEmail(form.elements[i].value),
                                             form.elements[i].name,
                                             form.elements[i].type);

            res = true;
            if(!res){
	        gtg++;
            }    
	}
        if(form.elements[i].name.indexOf("phone") > -1) {

            resultsTable[i] = new resultTable(isValidPhoneNumber(form.elements[i].value),
                                             form.elements[i].name,
                                             form.elements[i].type);

            res = true;
            if(!res){
	        gtg++;
            }  
	}
        //alert('blah '+i);
    }
   
    if(gtg > 0 && browser.indexOf("net") > -1) {
        alert('Please Check Your Form Values');
    }

    if(gtg == 0){
        return true;
    }
    return false;
}

function ValidateSignin(form) {
    var i,gtg,browser;
    var resultsTable = new Array();
    i = 0;
    gtg = 0;

    netscapeGtg = 0;
    browser = WhatBrowser();  
 
    for(i=0;i<form.elements.length;i++) {
        
        if(form.elements[i].name == "email") {
	    var e = "si_"+form.elements[i].name;
	    
	    resultsTable[i] = new resultTable(!isBlank(form.elements[i].value),
							  e,
							  form.elements[i].type);
            doCrossBrowserValidation(resultsTable[i],browser);
	    if(!resultsTable[i].result) {
               gtg++;
	    }
        }
	if(form.elements[i].name == "pass") {
	    var e = "si_"+form.elements[i].name;
	    resultsTable[i] = new resultTable(!isBlank(form.elements[i].value),
							  e,
							  form.elements[i].type);  
	    doCrossBrowserValidation(resultsTable[i],browser);
	    if(!resultsTable[i].result) {
               gtg++;
	    }
        }
    }
    if(gtg == 0) {
	if(netscapeGtg > 0) {
            alert('Please Check You Form Values');
	}
        return Boolean(true);
    }
    return Boolean(false);
}
