function empty_orderemail()
{
	if(document.getElementById("orderemail").value == "Email Address")
	   document.getElementById("orderemail").value ="";

   document.getElementById("orderemail").focus();
}

function set_orderemail()
{
if(document.getElementById("orderemail").value == "")
    document.getElementById("orderemail").value ="Email Address";
}

function empty_ordernumber()
{

if(document.getElementById("ordernumber").value == "Order Number")
   document.getElementById("ordernumber").value ="";
   document.getElementById("ordernumber").focus();
}

function set_ordernumber()
{
if(document.getElementById("ordernumber").value == "")
   document.getElementById("ordernumber").value ="Order Number";
}

function empty_username()
{
if(document.getElementById("username").value == "User Name")
   document.getElementById("username").value ="";

  document.getElementById("username").focus();
}

function set_username()
{
if(document.getElementById("username").value == "")
   document.getElementById("username").value ="User Name";
}

function empty_password()
{
if(document.getElementById("password").value == "Password")
   document.getElementById("password").value ="";

  document.getElementById("password").focus();
}

function set_password()
{
if(document.getElementById("password").value == "")
   document.getElementById("password").value ="Password";
}

function checkout ()
{
   alert('Checkout');
//   document.getElementById("checkout").value ="password...";
}

function search_order()
{
   document.getElementById("orderemail").value ="";
   document.getElementById("ordernumber").value ="";
}

function sort_products (sDropdown)
{
  document.apparel.sDropdown.value = sDropdown;
  document.getElementById('apparel').submit();
}



function isregvalid(regform)
{
	if (!validate_email('create','user_name'))
	{
		alert("Invalid Email");
//		document.getElementById('username').focus();
		return false;
	}
	return true;
}

function validate_email(form_id,username) {

   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   var address = document.forms[form_id].elements[username].value;

   if(reg.test(address) == false) {
//      alert('Invalid Email Address');
      return false;
   }
	return true;
}

function dosub()
{
	if( isregvalid(document.getElementById('create') ) )
    	document.getElementById('create').submit();

}

function dosub1(sItem)
{
	if( isregvalid1(document.getElementById('account') ) )
    {    	document.account.sItem.value=sItem;
	   	document.getElementById('account').submit();
    }
}

function isregvalid1(regform)
{
	if (!validate_email2('account','username1'))
	{
		alert("Invalid Email");
		document.getElementById('username1').focus();
		return false;
	}
	return true;
}
function validate_email2(form_id,username) {

   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   var address = document.account.username1.value;

   if(reg.test(address) == false) {
      return false;
   }
return true;
}


function setValues_user()
{
     document.report.b_name.value = document.report.sh_name.value;
     document.report.b_lname.value = document.report.sh_lname.value;
     document.report.b_email.value = document.report.sh_email.value;
     document.report.b_phone.value = document.report.sh_phone.value;
     document.report.b_address1.value = document.report.sh_address1.value;
     document.report.b_address2.value = document.report.sh_address2.value;
     document.report.b_city.value =  document.report.sh_city.value;
     document.report.b_state.value = document.report.sh_state.value;
     document.report.b_zip.value = document.report.sh_zip.value;
 }

function setValues_checkout()
{
     document.checkout.b_name.value = document.checkout.sh_name.value;
     document.checkout.b_lname.value = document.checkout.sh_lname.value;
     document.checkout.b_email.value = document.checkout.sh_email.value;
     document.checkout.b_phone.value = document.checkout.sh_phone.value;
     document.checkout.b_address1.value = document.checkout.sh_address1.value;
     document.checkout.b_address2.value = document.checkout.sh_address2.value;
     document.checkout.b_city.value =  document.checkout.sh_city.value;
     document.checkout.b_state.value = document.checkout.sh_state.value;
     document.checkout.b_zip.value = document.checkout.sh_zip.value;
 }

function goTo_submit()
{
   document.checkout.submit();   //submit the form
}


function goTo(sMsg)
{

   document.checkout.sMsg.value = sMsg;
   document.checkout.submit();   //submit the form
}

function goTo_remove(sBox)
{
   document.apparel.sBox.value = sBox;
   document.apparel.submit();   //submit the form
}


function goTo_item(sItem, type )
{
	if (sItem=='add_cart') {
		createCookie("showcart","1");
	}

	var sItem_qty = document.getElementById('quantity').value;

	   if(sItem_qty == 0 && type == 2 )
	    return;

	   document.itemform.sItem.value = sItem;

	   document.itemform.sItem.value = sItem;
	   document.itemform.submit();   //submit the form

}

function goTo_item_size(sItem_size)
{
   document.itemform.sItem_size.value = sItem_size;
}

function goTo_item_size(sItem_size)
{
   document.itemform.sItem_size.value = sItem_size;
}



function goTo_item_qty(sItem_qty)
{
   var btn = document.getElementById('add_button');
   if(sItem_qty > 0 )
        btn.disabled = false;
   	else
		btn.disabled = true;

   document.itemform.sItem_qty.value = sItem_qty;
}

function goTo_item_qty1(sItem_qty)
{
   if(sItem_qty > 0 )
        ;
   	else
	  alert('Please, select a size');
}

function check_contact_form()
{    if( document.getElementById("contact_name").value == '' )
    {   	    alert('First Name must be specified!');
       document.getElementById("contact_name").focus();
       		return false;
    }

    if( document.getElementById("contact_email").value == '' )
    {
   	    alert('E-Mail must be specified!');
       document.getElementById("contact_email").focus();
       		return false;
    }

    if( document.getElementById("contact_phone").value == '' )
    {
   	    alert('Phone must be specified!');
       document.getElementById("contact_phone").focus();
       		return false;
    }

    if( document.getElementById("contact_message").value == '' )
    {
   	    alert('Message must be specified!');
       document.getElementById("contact_message").focus();
       		return false;
    }

    if( document.getElementById("contact_email").value != '' )
    {
       if (!validate_contactus_email())
		{
			alert("Invalid Email");
			document.getElementById('contact_email').focus();
			return false;
		}
    }

     document.contact_us.submit();}

function check_item_email_form()
{
    if( document.getElementById("your_name").value == '' )
    {
   	    alert('Your Name must be specified!');
       document.getElementById("your_name").focus();
       		return false;
    }

    if( document.getElementById("your_email").value == '' )
    {
   	    alert('Your E-Mail must be specified!');
       document.getElementById("your_email").focus();
       		return false;
    }

    if( document.getElementById("friend_name").value == '' )
    {
   	    alert('Friend Name must be specified!');
       document.getElementById("friend_name").focus();
       		return false;
    }

    if( document.getElementById("friend_email").value == '' )
    {
   	    alert('Friend E-Mail must be specified!');
       document.getElementById("friend_email").focus();
       		return false;
    }

     document.item_email.submit();
}

function validate_contactus_email() {

   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   var address = document.contact_us.contact_email.value;

   //alert (reg.test(address)) ;

   if(reg.test(address) == false) {
      return false;
   }
return true;
}

function dosub_chekcout()
{

	if (isregstep2valid(document.getElementById('checkout')))
		goTo('review');
//	alert('it is ok');

}

function ValidatePhone1( n , type)
{
      string = document.getElementById(n).value;

	var new_string = '';
	var iChars = "0123456789()-";

	for (var i = 0; i < string.length; i++) {
	  c = string.charAt(i);
	  	if (iChars.indexOf(c) != -1)
	  	{
	  		  if ((c < "0") || (c > "9"))
	  		  ;
	  		  else
		    new_string =  new_string + c;
	  	}
  		else
	  	{
  	  	return false;
  	  	}
	  }

	   if(type ==2 )
		if(new_string.length < 10 )
		   return false;

    var new_number = "("+ new_string.substr(0,3) + ")" + new_string.substr(3,3) + "-" + new_string.substr(6);
    document.getElementById(n).value = new_number;
	return true;
}

function validate_email1(n) {

   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   var address = document.getElementById(n).value;

   if(reg.test(address) == false) {
//      alert('Invalid Email Address');
      return false;
   }
	return true;
}

function isregstep2valid(regform)
{
		for (var i=0; i < regform.elements.length-2; i++) {
			id=regform.elements[i].id;

			if(id=='coupon_code' || id== 'Shipping Address2' || id == 'Billing Address2')
			  continue;

			len=regform.elements[i].value.length;
			if (len==0)
			{
				alert("Please fill "+id);
				regform.elements[i].focus();
				return false;
			}
		}

		if( document.getElementById('Shipping Email').value != "" )
		if (!validate_email1('Shipping Email'))
		{
			alert("Invalid Shipping Email");
			document.getElementById('Shipping Email').focus();
			return false;
		}

		 if( document.getElementById('Shipping Phone').value != "" )
		if (!ValidatePhone1('Shipping Phone',2))
		{
			alert("Invalid Shipping Phone or wrong length");
			document.getElementById('Shipping Phone').focus();
			return false;
		}

		if( document.getElementById('Shipping State').value == "-1" )
		 {		 	     alert("Please select Shipping State");
		 	     document.getElementById('Shipping State').focus();
				return false;
		 }

 		if( document.getElementById('Shipping Zip').value != "" )
		if (!validate_zip1('Shipping Zip'))
		{
			alert("Invalid Shipping Zip or wrong length ( max 5 digits )");
			document.getElementById('Shipping Zip').focus();
			return false;
		}

		if( document.getElementById('Billing Email').value != "" )
		if (!validate_email1('Billing Email'))
		{
			alert("Invalid Billing Email");
			document.getElementById('Billing Email').focus();
			return false;
		}
			if( document.getElementById('Billing Phone').value != "" )
		if (!ValidatePhone1('Billing Phone',2))
		{
			alert("Invalid Billing Phone or wrong length");
			document.getElementById('Billing Phone').focus();
			return false;
		}

		 if( document.getElementById('Billing State').value == "-1" )
		 {
		 	     alert("Please select Billing State");
		 	     document.getElementById('Billing State').focus();
				return false;
		 }

	   if( document.getElementById('Billing Zip').value != "" )
		if (!validate_zip1('Billing Zip'))
		{
			alert("Invalid Billing Zip or wrong length ( max 5 digits )");
			document.getElementById('Billing Zip').focus();
			return false;
		}


	  if( document.getElementById('Credit Card Type').value == "-1" )
	 {
	 	     alert("Please select Credit Card Type");
	 	     document.getElementById('Credit Card Type').focus();
			return false;
	 }


	  if( document.getElementById('Expiration Month').value == "-1" )
	 {
	 	     alert("Please select Expiration Month");
	 	     document.getElementById('Expiration Month').focus();
			return false;
	 }

	 if( document.getElementById('Expiration Year').value == "-1" )
	 {
	 	     alert("Please select Expiration Year");
	 	     document.getElementById('Expiration Year').focus();
			return false;
	 }


 /*
if( document.getElementById('Home Phone Number').value != "" )
		if (!validate_phone('regform','Home Phone Number'))
		{
			alert("Invalid Home Phone Number or wrong length");
			document.getElementById('Home Phone Number').focus();
			return false;
		}

if (!validate_phone('regform','Mobile Phone Number'))
{
	alert("Invalid Cellular Phone Number or wrong length");
	document.getElementById('Cellular Phone Number').focus();
	return false;
}

if( document.getElementById('Business Phone Number').value != "" )
{
	 if (!validate_phone('regform','Business Phone Number'))
     {
       alert("Invalid Business Phone Number or wrong length");
       document.getElementById('Business Phone Number').focus();
       return false;
     }
}

if (!validate_email('regform','Email'))
{
	alert("Invalid Email");
	document.getElementById('Email').focus();
	return false;
}

if (document.getElementById(' Password').value!=document.getElementById('Password confirmation').value)
{
	alert("Password confirmation doesn't match to original password");
	document.getElementById('Password confirmation').focus();
	return false;
}

if (document.getElementById('terms').value.toLowerCase() != "yes" )
{
	alert("You must read the Terms and Conditions and type 'yes'");
	document.getElementById('terms').focus();
	return false;
}

*/
return  true;
}

function go_to_checkout( val ) {

if( val != 0)
	window.location.href='https://aggielandoutfitters.com/checkout.html?new=1';
else
  alert('Please, add items to you cart');
}



/* set phone number format */




function ValidatePhone( n )
{
	if (!ValidatePhone1(n, 1 ))
	{
		alert("Invalid "+ n + " or wrong length");
		document.getElementById(n).focus();
		return false;
	}
	p= document.getElementById(n).value;


	if(p.length==3)
	{
		pp=p;

		d4=p.indexOf('(')
		d5=p.indexOf(')')

		if(d4==-1){
			pp="("+pp;
		}

		if(d5==-1){
			pp=pp+")";
		}

		document.getElementById(n).value="";
		document.getElementById(n).value=pp;
	}

	if(p.length>3)
	{
		d1=p.indexOf('(')
		d2=p.indexOf(')')
		if (d2==-1)
		{
			l30=p.length;
			p30=p.substring(0,4);
			p30=p30+")"
			p31=p.substring(4,l30);
			pp=p30+p31;
			document.getElementById(n).value="";
			document.getElementById(n).value=pp;
		}
	}

	if(p.length>5){

		p11=p.substring(d1+1,d2);
		if(p11.length>3)
		{
			p12=p11;
			l12=p12.length;
			l15=p.length
			p13=p11.substring(0,3);
			p14=p11.substring(3,l12);
			p15=p.substring(d2+1,l15);
			document.getElementById(n).value="";
			pp="("+p13+")"+p14+p15;
			document.getElementById(n).value=pp;
		}

		l16=p.length;
		p16=p.substring(d2+1,l16);
		l17=p16.length;
		if(l17>3&&p16.indexOf('-')==-1)
		{
			p17=p.substring(d2+1,d2+4);
			p18=p.substring(d2+4,l16);
			p19=p.substring(0,d2+1);

			pp=p19+p17+"-"+p18;
			document.getElementById(n).value="";
			document.getElementById(n).value=pp;
		}
	}

  setTimeout(ValidatePhone,100)

}

function getIt(m)
{
	n=m.id;
	ValidatePhone(n )
}

function testphone(obj1){

	p=obj1.value;
	p=p.replace("(","")
	p=p.replace(")","")
	p=p.replace("-","")
	p=p.replace("-","")

	if (isNaN(p)==true){

		alert("Check phone");
		return false;
	}
}

function validate_zip1 ( n )
{
  var string = document.getElementById(n).value;

  if(string.length >5 )
  {
	 return false;
  }

    var new_string = '';
    for (i = 0; i < string.length; i++)
     {
        var c = string.charAt(i);
        if (((c < "0") || (c > "9") ) )
        {
            document.getElementById(n).value = new_string;
            return false;
        }
        else
		    new_string =  new_string + c;
     }
   return true;
}


function validate_zip ( m )
{
  n=m.id;

  var string = document.getElementById(n).value;
  var new_string = '';

    for (i = 0; i < string.length; i++)
     {
        var c = string.charAt(i);
        if (((c < "0") || (c > "9") ) )
        {
            document.getElementById(n).value = new_string;	        alert("Zip should contain only digits");
            return false;
        }
        else
		    new_string =  new_string + c;
     }
   return true;
}

function checkout_disable( type )
{
	var sItem_qty = document.getElementById('quantity').value;
	if(sItem_qty > 0 )
        ;
   	else
   	{   		if(type==2)
	  alert('Please, select a size');
	}


   var btn = document.getElementById('checkout_button');

		btn.value = "DISABLED";

	document.getElementById('checkout_button').disabled = true;
	document.getElementById('m4Parent').disabled = true;

}


function doUnload()
{
   window.location = 'http://aggielandoutfitters.com/destroySession.php?destroySession=true';
}

var ClosesWindow = true;
function CheckWindowClosed()
{
	window.location = 'https://aggielandoutfitters.com/destroySession.php';
//	window.open('https://aggielandoutfitters.com/destroySession.php');

}

//Cookies Handling...
function createCookie(name,value) {
	var date = new Date();
	var expiry = new Date(date.getTime() + 365 * 24 * 60 * 60 * 1000);
    var expires = "; expires="+expiry.toGMTString();
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

//ipafd and iphone fix
$(document).ready(function(){
if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i))) {
    $(".main-nav li a").click(function(){
        //we just need to attach a click event listener to provoke iPhone/iPod/iPad's hover event
        //strange
    });
}
});
