//Affiliate Js Scripts
function customerLog()
{
	var alertMessage = "";
		
	//Checking empty Fields
	if(document.getElementById("username").value == "") { alertMessage = alertMessage + "*Username is empty\n"; }
	if(document.getElementById("password").value == "") { alertMessage = alertMessage + "*Password is empty \n"; }
		
	//Check emails 
	emailRE = new RegExp("^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$");
	if(!emailRE.test(document.getElementById("username").value)) { alertMessage = alertMessage + "*Invalid email \n"; }
	
	if(alertMessage=="") {
		
		//Ajax Check User
		var postVars = 'action=checkUserCustomer&username='+document.getElementById("username").value+'&password='+document.getElementById("password").value;
		ajaxObj.call(postVars, respCheckUser);
						
	} else { 
		
		alert(alertMessage); 
		
	}	
}

//Check Ajax User
function respCheckUser(resp)
{
	if (resp == 0)
	{
		alert('Unfortunately, the username and password combination you entered does not match. Please verify the information you entered or click Forgot Password to have your password emailed to you.');
	} else if (resp == 1) {
		alert('Unfortunately, that email address does not exists in our database. Please verify your email address');
	} else {
		document.getElementById('customerLogin').submit();
	}

}

function getCustomer() {
	
	var alertMessage = "";
		
	//Checking empty Fields
	if(document.getElementById("username").value == "") { alertMessage = alertMessage + "*Username is empty\n"; }
	if(document.getElementById("password").value == "") { alertMessage = alertMessage + "*Password is empty \n"; }
		
	//Check emails 
	emailRE = new RegExp("^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$");
	if(!emailRE.test(document.getElementById("username").value)) { alertMessage = alertMessage + "*Invalid email \n"; }
	
	if(alertMessage=="") {
		
		//Ajax Check User
		var postVars = 'action=getCustomer&username='+document.getElementById("username").value+'&password='+document.getElementById("password").value;
		ajaxObj.call(postVars, respGetCustomer);
		
						
	} else { 
		
		alert(alertMessage); 		
		
	}	
}

function getCustomerLogged(user,passwd) {

	document.getElementById('ship[answered]').value = '0';
	document.getElementById('bill[answered]').value = '0';
			
	//Ajax Check User
	var postVars = 'action=getCustomer&username='+user+'&password='+passwd;
	ajaxObj.call(postVars, respGetCustomer);
			
}

function respGetCustomer(resp) {
	
	if(resp == 1) { 
		alert('Unfortunately, the password you entered does not match our database.  Please verify your password and try again.');
		
	} else {
			
			//Bill
			document.getElementById('bill[fname]').value 	= resp['customerBillfname'];
			document.getElementById('bill[lname]').value 	= resp['customerBilllname'];
			document.getElementById('bill[email]').value 	= resp['customerBillemail'];
			document.getElementById('bill[phone]').value 	= resp['customerBillphone'];
			document.getElementById('bill[company]').value 	= resp['customerBillcompany'];
			document.getElementById('bill[address1]').value = resp['customerBilladdress1'];
			document.getElementById('bill[address2]').value = resp['customerBilladdress2'];
			document.getElementById('bill[city]').value 	= resp['customerBillcity'];
			document.getElementById('bill[province]').value = resp['customerBillprovince'];
			document.getElementById('bill[zipcode]').value 	= resp['customerBillzipcode'];
			
			//Select State
			totalOptions = parseFloat(document.getElementById('bill[state]').length)-1;
			for(x=0;x<=totalOptions;x++) {
				if(document.getElementById('bill[state]').options[x].value == resp['stateSelect1']) {
					document.getElementById('bill[state]').selectedIndex = x;
				}
			}
			
			//Select country
			totalOptions = parseFloat(document.getElementById('bill[country]').length)-1;
			for(x=0;x<=totalOptions;x++) {
				if(document.getElementById('bill[country]').options[x].value == resp['countrySelect1']) {
					document.getElementById('bill[country]').selectedIndex = x;
				}
			}
		
		//Display ship
		if(resp['customerShipid'] >= 1) {
			document.getElementById('addressTable2').style.display = '';document.getElementById('diffShip').click();	
			//Ship
			document.getElementById('ship[fname]').value 	= resp['customerShipfname'];
			document.getElementById('ship[lname]').value 	= resp['customerShiplname'];
			document.getElementById('ship[email]').value 	= resp['customerShipemail'];
			document.getElementById('ship[phone]').value 	= resp['customerShipphone'];
			document.getElementById('ship[company]').value 	= resp['customerShipcompany'];
			document.getElementById('ship[address1]').value = resp['customerShipaddress1'];
			document.getElementById('ship[address2]').value = resp['customerShipaddress2'];
			document.getElementById('ship[city]').value 	= resp['customerShipcity'];
			document.getElementById('ship[province]').value = resp['customerShipprovince'];
			document.getElementById('ship[zipcode]').value 	= resp['customerShipzipcode'];

			//Select State
			totalOptions = parseFloat(document.getElementById('ship[state]').length)-1;
			for(x=0;x<=totalOptions;x++) {
				if(document.getElementById('ship[state]').options[x].value == resp['stateSelect2']) {
					document.getElementById('ship[state]').selectedIndex = x;
				}
			}
			
			//Select country
			totalOptions = parseFloat(document.getElementById('ship[country]').length)-1;
			for(x=0;x<=totalOptions;x++) {
				if(document.getElementById('ship[country]').options[x].value == resp['countrySelect2']) {
					document.getElementById('ship[country]').selectedIndex = x;
				}
			}
		}
		
		document.getElementById('bill[assignNewAddress]').value = '1';
		document.getElementById('ship[assignNewAddress]').value = '1';
		
		//Run Functions
		validateCouponEmail();
		updateValidationFlag();
		resetShipping();
		switchAddress();	
		
	}
}

//diffShip
function showShippingCustomer() {
	
	if(document.getElementById('diffShip').checked) {
		document.getElementById('addressTable2').style.display = '';		
	} else {
		document.getElementById('addressTable2').style.display = 'none';
	}
	
}

//Check Password
function checkPassword() {

	str = document.getElementById('password').value;
	
	var er = new RegExp(/[A-Za-z]/);
	var er2 = new RegExp(/[0-9]/);
    if(er.test(str) && er2.test(str)){ document.getElementById('formCustomer').submit(); } else { alert("Password not a valid"); }
	
    
}

function orderHistoryItem(id,lastId) {
	
	total = parseFloat(document.getElementById('quantity'+id).value)*parseFloat(document.getElementById('price'+id).value);
	
	if(!isNaN(total))
		document.getElementById('cost'+id).innerHTML = total.toFixed(2);
	else
		document.getElementById('cost'+id).innerHTML = '00.00';
	
	
	var totalCost = 0;
	
	for(x=1;x<=lastId;x++) {
		
		tmpTotal = parseFloat(document.getElementById('quantity'+x).value)*parseFloat(document.getElementById('price'+x).value);
		
		if(!isNaN(tmpTotal))
			totalCost = totalCost +  tmpTotal;
		
	}
	
	document.getElementById('totalCost').innerHTML = totalCost.toFixed(2);
	
}


function editShipAddressBill() {
	if(document.getElementById('bill[answered]').value == 0) {
		if(confirm('Should we use this address as your new shipping address for your account?'))
		{
			document.getElementById('bill[assignNewAddress]').value = '0';
			document.getElementById('bill[answered]').value = '1';
		}
		else
		{
			document.getElementById('bill[assignNewAddress]').value = '1';
			document.getElementById('bill[answered]').value = '1';
		}
	}
}

function editShipAddressShip() {
	if(document.getElementById('ship[answered]').value == 0) {
		if(confirm('Should we use this address as your new billing address for you account?')) 
		{
			document.getElementById('ship[assignNewAddress]').value = '0';
			document.getElementById('ship[answered]').value = '1';
		}
		else
		{
			document.getElementById('ship[assignNewAddress]').value = '1';
			document.getElementById('ship[answered]').value = '1';
		}
	}
}

//Back End Functions
function editCustomer() {
	if(!checkPermission(43))
	{
		alert(language['accessDenied']);
			return
	}
	
	document.getElementById('customerList').action = "powerup.php?page=customer/index.php&action=profile";
	document.getElementById('customerList').submit();
}

function deleteCustomer() {
	if(!checkPermission(44))
	{
		alert(language['accessDenied']);
			return
	}
	
	if(confirm("Are you sure?")) {
		document.getElementById('customerList').action = "powerup.php?page=customer/index.php&action=delete";
		document.getElementById('customerList').submit();
	}
}

function customerOrderHistory() {
	document.getElementById('formCustomer').action = "powerup.php?page=customer/index.php&action=orderhistory";
	document.getElementById('formCustomer').submit();
}

//Review Functions

//Edit Review
function reviewEdit() {
	document.getElementById('formCustomer').action = "powerup.php?page=customer/review.php&action=edit";
	document.getElementById('formCustomer').submit();
}

//Delete Review
function reviewDelete() {
	if(confirm("Are you sure?")) {
		document.getElementById('formCustomer').action = "powerup.php?page=customer/review.php&action=delete";
		document.getElementById('formCustomer').submit();
	}
}

//Save Review
function reviewSave() {
	document.getElementById('formCustomer').action = "powerup.php?page=customer/review.php&action=save";
	document.getElementById('formCustomer').submit();
}