// ====== Rollovers ============================

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

// ====== <end> Rollovers ============================

function ValidateRegisterForm(){
	
	if (!checkField('nickname',	'Please type a nickname'))	return false;
	
	if (!checkEmail(document.getElementById('email').value)){
		alert("Please make sure you typed a valid email address.")
		return false;
	}
	
	if (!checkField('password',		'Please type your password'))	return false;
	if (!checkField('password2',	'Please type your password twice'))	return false;
	if (document.getElementById('password').value!=document.getElementById('password2').value){
		alert("Please type your password twice")
		return false;	
	}
	
	if (!checkField('firstname',	'Please type your first name'))	return false;
	if (!checkField('lastname',	'Please type your last name'))	return false;
	if (!checkField('address',	'Please type your address'))	return false;
	if (!checkField('city','Please type your city'))			return false;
	if (!checkField('country','Please select your country'))	return false;
	if (!checkField('state','Please select your state/province'))	return false;
	if (!checkField('zip','Please type your zip code'))		return false;
	
}

		    	
function ValidateShippingForm(){
	
	if (!checkField('delivery_fullname','Please type your full name'))	return false;
	if (!checkField('delivery_email',	'Please make sure you typed a valid email address.'))	return false;
	if (!checkEmail(document.getElementById('delivery_email').value)){
		alert("Please make sure you typed a valid email address.")
		return false;
	}
	
	
	if (!checkField('delivery_address',	'Please type your address'))	return false;
	if (!checkField('delivery_city','Please type your city'))			return false;
	if (!checkField('delivery_state',	'Please select your state'))	return false;
	if (!checkField('delivery_country','Please select your country'))	return false;
	if (!checkField('delivery_zip','Please type your zip code'))		return false;
	
	//if (!TestInputType(document.getElementById('delivery_zip'),"[^0-9]",'Please use numbers only !',	"Only digits allowed ")) return false;
	
	if (!checkField('delivery_phone','Please type your phone number'))	return false;
	return true;
	
}

function checkCVV(v){
	if (v.length<3){
		alert("Your CVV value should have at least 3 digits");
		return false;
	}
	return true;
}

function ValidateBillingForm(){
	if (document.getElementById('payment_method').value!='paypal'){
		ccn = document.getElementById('payment_cc_number').value;
		cvv = document.getElementById('payment_cc_cvv').value;
	
		if (!checkField('payment_cc_number','Please type your credit card number')) return false;
		if (!ccn) { alert('Your credit card number is invalid or was not entered');return false;}
		if (!checkField('payment_cc_month','Please select your credit card expire date') || !checkField('payment_cc_year','Please select your credit card expire date')) return false;
		if (!checkField('payment_cc_cvv','Please type your CVV')) return false;
		if (!checkCVV(cvv)) return false;
	}


	if (!checkField('billing_fullname','Please type your full name'))	return false;
	if (!checkField('billing_email',	'Please make sure you typed a valid email address.'))	return false;
	if (!checkEmail(document.getElementById('billing_email').value)){alert("Please make sure you typed a valid email address.");return false;}
	if (!checkField('billing_address',	'Please type your address'))	return false;
	if (!checkField('billing_city','Please type your city'))			return false;
	if (!checkField('billing_state',	'Please select your state'))	return false;
	if (!checkField('billing_country','Please select your country'))	return false;
	if (!checkField('billing_zip','Please type your zip code'))		return false;
	if (!checkField('billing_phone','Please type your phone number'))	return false;
	return true;
	
}

function validateCreditCard(s) {
	function cardval(s) {
		// remove non-numerics
		var v = "0123456789";
		var w = "";
		for (i=0; i < s.length; i++) {
		x = s.charAt(i);
		if (v.indexOf(x,0) != -1)
		w += x;
		}
		// validate number
		j = w.length / 2;
		if (j < 6.5 || j > 8 || j == 7) return false;
		k = Math.floor(j);
		m = Math.ceil(j) - k;
		c = 0;
		for (i=0; i<k; i++) {
		a = w.charAt(i*2+m) * 2;
		c += a > 9 ? Math.floor(a/10 + a%10) : a;
		}
		for (i=0; i<k+m; i++) c += w.charAt(i*2+1-m) * 1;
		return (c%10 == 0);
	}
}

					
function checkEmail(str) {
		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    return false
		 }

 		 return true					
}


function TestInputType(objValue,strRegExp,strError,strDefaultError)
{
   var ret = true;

    var charpos = objValue.value.search(strRegExp);
    if(objValue.value.length > 0 &&  charpos >= 0)
    {
     if(!strError || strError.length ==0)
      {
        strError = strDefaultError;
      }//if
      alert(strError + "\n [Error character position " + eval(charpos+1)+"]");
      ret = false;
    }//if
 return ret;
}

function checkField(id,message){
	var v = document.getElementById(id).value;
	if (v==''){
		alert(message);									    						
		return false;
	}
	return true;
}
		    	
		   
function checkQuantity(el,value){
	
	q = parseInt(el.value);
	
	var v = "0123456789";
	for (i=0; i < q.length; i++) {
		x = q.charAt(i);
		
		if (v.indexOf(x,0) <0){
			alert("Please type a valid quantity");
			return 0;
		}
	}
	
	
	if (q>value && value!=0) {
		alert("You cannot order more then max. allowed quantity.");
		el.value = value;
		return 0;
	}	
	return 1;
}


