// JavaScript Document
function isNull(val){return(val==null);}

function emptyCartWarning(){
	new Insertion.After('header', '<div id=\"errheader\" class=\"error\" style=\"margin-left: 450px;\">This will empty your cart! <form><input type=\"button\" style=\" cursor: hand; cursor: pointer;\" value=\"Empty Cart\"  onClick=\"window.location=\'cart.php?emptycart=ALL\'\"></form><form><input type=\"button\" style=\" cursor: hand; cursor: pointer;\" value=\"Cancel\" onClick=\"window.location.reload(true);\"></form></div>');
}

/*<script>

	window.alert("Fast popup coming now...");
	window.alert("And one with a pause of two seconds...");

	sleep(2000);

	window.alert("Thank you for your attention!");

	function sleep(timeout)
	{
		window.showModalDialog("javascript:document.writeln('<script>window.setTimeout(function () { window.close(); }, " + timeout + "); <\/script>');");
	}

</script>
*/

var question_number=-1;
var ShipTo=-1;

function validate_required($field, $errorText){
	$required = $F($field)
	if ($required==null||$required==""||$required=="Make a Selection..." || $required=="not"){
		new Insertion.After($field, '<div id=\"err' + $field + '\" class=\"error\">Required! ' + $errorText + '</div>');
		$($field).focus()
		return 1;
	}else{
		return 0;
	}
}

function validate_email($field, $errorText){
	$Email = $F($field)
	apos=$Email.indexOf("@")
	dotpos=$Email.lastIndexOf(".")
	if (apos<1||dotpos-apos<2){
		new Insertion.After($field, ' <div id=\"err' + $Email + '\" class=\"error\">Invalid!' + $errorText + '</div>');
		$($field).focus()
		return 1;
	}else{
		return 0;
	}
}

/*
function validate_addr_nopo($field, $errorText){
	$boxes=$F($field).match(/PO/g);
	if($boxes==null){
		$boxes=$F($field).match(/P.O./ig);
	}
    if($boxes==null){
        $boxes=$F($field).match(/P. O./ig);
    }
    if($boxes==null){
        $boxes=$F($field).match(/box/ig);
    }
	if($boxes!=null){
		//alert("validate_addr_nopo boxes.length=" + $boxes.length + " boxes=" + $boxes + "\n");
        new Insertion.After($field, ' <div id=\"err' + $F($field) + '\" class=\"error\">Invalid! ' + $errorText + '</div>');
        $($field).focus()
        return 1;
    }else{
        return 0;
    }
}
*/

function validate_length($field,$len,$errorText){
    if ($F($field).length != $len)
        {
		new Insertion.After($field, ' <div id=\"err' + $F($field) + '\" class=\"error\">Invalid! ' + $errorText + '</div>');
        return (1);
        }
    return (0);
}

function values_match($field1, $field2, $errorText){
	if($F($field1) != $F($field2)){
		new Insertion.After($field2, ' <div id=\"err' + $email + '\" class=\"error\">Invalid!' + $errorText + '</div>');
		$($field2).focus()
		return 1;
	}else{
		return 0;	
	}
}

function validate_newaccount(){
	$aryElements = $$('div.error');

        for(var $i=0, $len=$aryElements.length; $i<$len; $i++ ){
                Element.remove($aryElements[$i]);
        }

        var $aryError = new Array();
        $aryError[0] = validate_required('email', 'Enter an e-mail address.');
        $aryError[1] = validate_required('pass', 'Enter a password.');
	if($aryError[0] == 0){
	        $aryError[0] = validate_email('email', '  e-mail address!');
	}
	var $error = 0;
        for(var $i=0; $i < $aryError.length; $i++){
                $error = $error + $aryError[$i];
        }

        if($error > 0){
                return false;
        }else{
                return true
        }
	return false;
}

function validate(){
	
	$aryElements = $$('div.error');
	
	for(var $i=0, $len=$aryElements.length; $i<$len; $i++ ){
		Element.remove($aryElements[$i]);
 	}
	//alert("fldHowFound=" + $F('fldHowFound') + " question_num=" + $F('question_num'));
	var $aryError = new Array();
	$aryError[0] = validate_required('fldFirstName', 'Please enter your first name.');
	$aryError[1] = validate_required('fldLastName', 'Please enter your last name.');
	$aryError[2] = validate_required('fldAddress1', 'Please enter your address');
/*
	if($aryError[2] == 0){
		$aryError[2] = validate_addr_nopo('fldAddress1', 'We do not deliver to PO Boxes. Enter a street address.');
	}
*/
	$aryError[3] = validate_required('fldCity', 'Please enter your city.');
	$aryError[4] = validate_required('fldState', 'Please enter your state.');
	$aryError[5] = validate_required('fldPostalCode', 'Enter a postal code.');
	$aryError[6] = validate_required('fldPhone', 'Enter a phone number.');
	if($aryError[6] == 0){
		$aryError[6]=validate_length('fldPhone', 12, 'Enter a valid US phone number.');
	}


	$aryError[7] = validate_required('fldEmail', 'Enter an e-mail address.');
	if($aryError[7] == 0){
		$aryError[8] = validate_email('fldEmail', ' Enter an e-mail address.');
	}else{
		$aryError[8] = 0;
	}
	$aryError[9] = 0;
	if(document.forms.requestForm.fldHowFound.selectedIndex==0){
		//alert("howfound="+$howfound);
		new Insertion.After($('divHow'), '<div id=\"errHowFound\" class=\"error\">Required! Tell us how you found us.</div>');
		document.forms.requestForm.fldHowFound.focus();
		$aryError[9] = 1;
	}else{
		$aryError[9] = 0;
	}
    $aryError[10] = 0;
	if(question_number==-1){
		new Insertion.After($('questionno'), '<div id=\"errquestion_num\" class=\"error\">Required! Select a question.</div>');
        document.forms.requestForm.question_num[0].focus();
		$aryError[10] = 1;
	}else{
		$aryError[10] = 0;
	}
	$aryError[11] = 0;
	/*$aryError[11] = validate_required('fldShipTo', 'Required! Select a address type.');
	alert("fdShipTo=" . $('fldShipTo'));*/
    if(ShipTo==-1){
        new Insertion.After($('shipto'), '<div id=\"errshipto\" class=\"error\">Required! Select a address type.</div>');
        document.forms.requestForm.fldShipTo[0].focus();
        $aryError[11] = 1;
    }else{
        $aryError[11] = 0;
    }
	var $error = 0;
//alert("array length= " + $aryError.length + "\n");

	for(var $i=0; $i < $aryError.length; $i++){
		$error = $error + $aryError[$i];
//alert("aryError[" + $i + "] error=" + $error + "\n");
	}

//alert("error count is " + $error + "\n"); 
	if($error > 0){
		//alert("return false");
		return false;
	}else{
		//alert("return true");
		return true
	}
	return false;
}


