/*---------------------------------------------------------------
**   Common scripts
**
**--------------------------------------------------------------*/
function isEmail(str){
    var objRegExp =  /^((?:(?:(?:(\w|~)[~\.\-\+]?)*)(\w|~))+)\@((?:(?:(?:\w[\.\-\+]?){0,62})\w)+)\.([a-zA-Z]{2,6})$|^$/;
    return objRegExp.test(str);
} 

function isDate(str){
	var objRegExp = /^(?:(?:0?[1-9]|1\d|2[0-8])(\/|-)(?:0?[1-9]|1[0-2]))(\/|-)(?:[1-9]\d\d\d|\d[1-9]\d\d|\d\d[1-9]\d|\d\d\d[1-9])$|^(?:(?:31(\/|-)(?:0?[13578]|1[02]))|(?:(?:29|30)(\/|-)(?:0?[1,3-9]|1[0-2])))(\/|-)(?:[1-9]\d\d\d|\d[1-9]\d\d|\d\d[1-9]\d|\d\d\d[1-9])$|^(29(\/|-)0?2)(\/|-)(?:(?:0[48]00|[13579][26]00|[2468][048]00)|(?:\d\d)?(?:0[48]|[2468][048]|[13579][26]))$/;
	return objRegExp.test(str);
} 
function isCompleted(str){
	return (str.length > 0)
}
function isSelected(opt){
	return (opt.selectedIndex > 0)
}
function checkEnter(e){
	var characterCode

	if(e && e.which){
		e = e
		characterCode = e.which
	}
	else{
		e = event
		characterCode = e.keyCode 
	}	
	if(characterCode == 13){
		checkComplete(); 
		return false 
	}
	else{
		return true 
	}
}
function checkComplete(){
	var email = document.forms["frmLogin"].strEmail.value;
	var password = document.forms["frmLogin"].strPassword.value;
	if (email.length > 0 && password.length > 0){
		validateLogin();
	}
}
	
function logout(){
	if(confirm('Click OK to logout from your account')) {
		document.location.href='/recmedia/rewards/user/logout.asp';
	}
}

function filterCategory(frm){
	var slt;
	slt = frm.intRCategory.selectedIndex;
	if (slt > 0) return true;
	return false;
}

function validateReg(frm){
	with(frm){
		
		
		if (!(isSelected(intTitleID))){
			alert ('Please select your title');
			return false;		
		}	
		if (!(isCompleted(strFirstName.value))) {
			alert ('Please enter your first name');
			return false;		
		}
		if (!(isCompleted(strSurname.value))) {
			alert ('Please enter your surname');
			return false;		
		}
		if (!(isCompleted(strCompany.value))) {
			alert ('Please enter your company');
			return false;		
		}
		if (!(isCompleted(strBuildingNameNo.value))) {
			alert ('Please enter your building number');
			return false;		
		}
		if (!(isCompleted(strAddress1.value))) {
			alert ('Please enter the first line of your address');
			return false;		
		}
		if (!(isCompleted(strPostcode.value))) {
			alert ('Please enter your postcode');
			return false;		
		}
		if (!(isCompleted(strPhone.value))) {
			alert ('Please enter your phone number');
			return false;		
		}
		if (!(isCompleted(strEmail.value))) {
			alert ('Please enter your email address');
			return false;		
		}
		if (!(isEmail(strEmail.value))) {
			alert ('Please enter a valid email address');
			return false;		
		}
		if (!(isCompleted(strPassword.value))) {
			alert ('Please enter your password (greater than 6 characters)');
			return false;		
		}
		if (strPassword.value.length < 6) {
			alert ('Please enter your password (greater than 6 characters)');
			return false;		
		}
		if (!(isCompleted(strPasswordConfirm.value))) {
			alert ('Please re-enter your password');
			return false;		
		}
		if (!(isCompleted(strPasswordConfirm.value))) {
			alert ('Please re-enter your password');
			return false;		
		}
		//Validate form elements
		if (strPassword.value != strPasswordConfirm.value) {
			alert ('Your passwords do not match');
			return false;		
		}
				
		if (!intAgree.checked) {
			alert ('Please confirm you had read the terms and conditions');
			return false;
		}
		//alert('Please complete all fields');
		postback.value = 1;
		return true;
	}
}

function validateAM(frm){
	with(frm){
		
		if (!(isCompleted(strEmail.value))) {
			alert ('Please enter your email address');
			return false;		
		}
		if (!(isEmail(strEmail.value))) {
			alert ('Please enter a valid email address');
			return false;		
		}
		if (!(isCompleted(strPassword.value))) {
			alert ('Please enter your password (greater than 6 characters)');
			return false;		
		}
		if (strPassword.value.length < 6) {
			alert ('Please enter your password (greater than 6 characters)');
			return false;		
		}
		//alert('Please complete all fields');
		postback.value = 1;
		return true;
	}
}

function validatePersonal2(frm){
	with(frm){
		if (!(isCompleted(strEmail.value))) {
			alert ('Please enter your email address');
			return false;		
		}
		if (!(isEmail(strEmail.value))) {
			alert ('Please enter a valid email address');
			return false;		
		}
		if (strPassword.value.length > 0) {
			if (strPassword.value.length < 6) {
				
				alert ('Please enter your new password (greater than 6 characters)');
				return false;		
			}
			if (!(isCompleted(strPasswordConfirm.value))) {
				alert ('Please re-enter your password');
				return false;		
			}
			if (strPassword.value != strPasswordConfirm.value) {
				alert ('Your passwords do not match');
				return false;		
			}		
		}		
		if (!(isSelected(intTitleID))){
			alert ('Please select your title');
			return false;		
		}	
		if (!(isCompleted(strFirstName.value))) {
			alert ('Please enter your first name');
			return false;		
		}
		if (!(isCompleted(strSurname.value))) {
			alert ('Please enter your surname');
			return false;		
		}
		if (!(isCompleted(strCompany.value))) {
			alert ('Please enter your company');
			return false;		
		}
		if (!(isCompleted(strBuildingNameNo.value))) {
			alert ('Please enter your building number');
			return false;		
		}
		if (!(isCompleted(strAddress1.value))) {
			alert ('Please enter the first line of your address');
			return false;		
		}
		if (!(isCompleted(strPostcode.value))) {
			alert ('Please enter your postcode');
			return false;		
		}
		if (!(isCompleted(strPhone.value))) {
			alert ('Please enter your phone number');
			return false;		
		}
		//alert('Please complete all fields');
		postback.value = 1;
		return true;
	}
}

function validateLogin(){
	frm = document.forms["frmLogin"];
	with (frm){
		postback.value = 1;
		submit();
	}
}

function validatePassReq(frm){
	with(frm){
		
		//Validate form elements
	
	
		
	
		//alert('Please complete all fields');
		frm.postback.value = 1;
		return true;
	}
}

function validateCart(frm){
	with(frm){
		if(strCart.value.length == 0){
			alert('There is nothing in your basket to submit')
		} else {
			if(confirm('Click OK to submit this claim. Please note\nclaims may take upto 14 days to process')){
				frm.submit();
			}	
		}
		
	}
}

function validateClaim(frm){
	with(frm){
		if (frm.strInvoiceNo.value.length == 0){
			alert('Please enter the invoice number before submitting a claim');
			return false;
		} else if (frm.strInvoiceDate.value.length == 0){
			alert('Please enter the invoice date before submitting a claim');
			return false;
		} else if (!isDate(frm.strInvoiceDate.value)){
			alert('Please enter a valid invoice date before submitting a claim');
			return false;
		}else if (frm.strClaim.value.length == 0){
			alert('Please add some items to your claim before submitting');
			return false;
		}
		//frm.postback.value = 1;
		submitClaim(frm);
		return false;
		
	}
}

function removeCartItem(intArrLoc) {
	var strList;
	if(confirm('Click OK to remove this item')){
		//strList = document.getElementById("strCart").value;
		document.location.replace('/recmedia/rewards/process/cart.asp?remove=1&loc=' + intArrLoc);
	}
}
function addCartItem(iPID,iQty) {
	var strList,intProductID,intQuantity;
	
	//This will not work on other pages
	//strList = document.getElementById("strCart").value;
	document.getElementById("intProductID").value = iPID;
	document.getElementById("intQuantity").value = iQty;
	
	intProductID = document.getElementById("intProductID").value;
	intQuantity = document.getElementById("intQuantity").value;

	if (intProductID.length == 0){
		alert('Please first select an item to add to the cart')
	} else if (intQuantity.length == 0) {
		alert('Please enter quantity required')
	} else {
		document.location.replace('/recmedia/rewards/process/cart.asp?add=1&pid=' + intProductID + '&qty=' + intQuantity);
	}
}
function addCartItem1() {
	var strList,intProductID,intQuantity;
		
	intProductID = document.getElementById("intProductID").value;
	intQuantity = document.getElementById("intQuantity").value;

	if (intProductID.length == 0){
		alert('Please first select an item to add to the cart')
	} else if (intQuantity.length == 0) {
		alert('Please enter quantity required')
	} else {
		document.location.replace('/recmedia/rewards/process/cart.asp?add=1&pid=' + intProductID + '&qty=' + intQuantity);
	}
}


function removeItem(intArrLoc) {
	var strList;
	if(confirm('Click OK to remove this item')){
		strList = document.getElementById("strClaim").value;
		strInvoice = document.getElementById("strInvoiceNo").value;
		document.location.replace('/recmedia/rewards/process/claim.asp?remove=1&loc=' + intArrLoc + '&claim=' + strList + '&invoice=' + strInvoice);
	}
}

function clearClaim() {
	if(confirm('Click OK to clear this claim')){
		strInvoice = document.getElementById("strInvoiceNo").value;
		document.location.replace('/recmedia/rewards/process/claim.asp?removeall=1' + '&invoice=' + strInvoice);
	}
}

function clearCart(frm) {	
	if(frm.strCart.value.length == 0){
			alert('There is nothing in your basket to empty')
	} else {
		if(confirm('Click OK to empty basket')){
			document.location.replace('/recmedia/rewards/process/cart.asp?removeall=1');
		}
	}
}

function submitClaim(frm) {
	if(confirm('Click OK to submit this claim')){
		frm.submit();
	}
}
function addItem() {
	var strList,intProductID,intQuantity;
	strList = document.getElementById("strClaim").value;
	intProductID = document.getElementById("intProductID").value;
	intQuantity = document.getElementById("intQuantity").value;
	strInvoice = document.getElementById("strInvoiceNo").value;
	strInvoiceDate = document.getElementById("strInvoiceDate").value;
	if (intProductID.length == 0){
		alert('Please first select an item from the invoice')
	} else if (intQuantity.length == 0) {
		alert('Please enter quantity you have purchased from the invoice')
	} else {
		document.location.replace('/recmedia/rewards/process/claim.asp?add=1&pid=' + intProductID + '&qty=' + intQuantity + '&claim=' + strList + '&date=' + strInvoiceDate + '&invoice=' + strInvoice );
	}
}
function addItem2() {
	var strList,intProductID,intQuantity;
	strList = document.getElementById("strClaim").value;
	intProductID = document.getElementById("intProductID").value;
	intQuantity = document.getElementById("intQuantity").value;
	strInvoice = document.getElementById("strInvoiceNo").value;
	strInvoiceDate = document.getElementById("strInvoiceDate").value;
	if (intProductID.length == 0){
		alert('Please first select an item from the invoice')
	} else if (intQuantity.length == 0) {
		alert('Please enter quantity you have purchased from the invoice')
	} else {
		document.location.replace('/recmedia/rewards/process/claim2.asp?add=1&pid=' + intProductID + '&qty=' + intQuantity + '&claim=' + strList + '&date=' + strInvoiceDate + '&invoice=' + strInvoice );
	}
}
