/**
 * Sets a Cookie with the given name and value.
 *
 * name       Name of the cookie 
 * value      Value of the cookie
 * [expires]  Expiration date of the cookie (default: end of current session)
 * [path]     Path where the cookie is valid (default: path of calling document)
 * [domain]   Domain where the cookie is valid
 *              (default: domain of calling document)
 * [secure]   Boolean value indicating if the cookie transmission requires a
 *              secure transmission
 */
 
 
/*
 var a=window.alert;
 window.alert=function(q) {var winHandle=window.open('../Popup.html?'+q,
  'mywindow','alwaysRaised=yes,status=no,width=287,height=185,left = '+(screen.width-286)/2 +',top = '+ (screen.height-184)/2);
  if (winHandle){ winHandle.focus();}
  if(winHandle==null){
	a(q);
  }
  } 
*/


window.alert=function(p,q){
	if (q==null){
		Dialog.alert(p, {windowParameters: {width:287, height:185}, okLabel: " Close", ok:function(win) {return true;}});
	}
	else Dialog.alert(p, {windowParameters: {width:287, height:185}, okLabel: "Close", ok:function(win) {window.location.replace(q); return true;}});
}

 
function setCookie(name, value, expires, path, domain, secure)
{
    document.cookie= name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

/**
 * Gets the value of the specified cookie.
 *
 * name  Name of the desired cookie.
 *
 * Returns a string containing value of specified cookie,
 *   or null if cookie does not exist.
 */
function getCookie(name)
{
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1)
    {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    }
    else
    {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1)
    {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}




function popup(mylink, windowname)
{
if (! window.focus)return true;
var href;
if (typeof(mylink) == 'string')
   href=mylink;
else
   href=mylink.href;
window.open(href, windowname, 'width=400,height=200,scrollbars=yes');
return false;
}

/**
	Check the language cookie
 */
function checkLang(value){
	if(getCookie("lang") == null){
		setCookie('lang',value,'','','','');
	}
	else if (getCookie("lang") == value){
		return true;
	}
	else
		return false;
}


function validateEmail(str) {
  //   var emailReg = "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";
  //   var regex = new RegExp(emailReg);
  //   return regex.test(src);
  			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	
  
  
  
  
  
  }


/**
	Check the Registration Form
 */
function checkEmptyReg(form1){
	var returnVal = true;				
	if ((document.form1.Card_number.value).length == 0){
		alert("<br><br>You have to populate the Card number!" );
		document.form1.Card_number.focus(); 
		returnVal = false;
	}
	else if ((document.form1.Title.value).length == 0){
		alert("<br><br>You have to specify the Title!" );
		document.form1.Title.focus();
		returnVal = false;
	}				
	else if ((document.form1.firstname.value).length == 0){
		alert("<br><br>You have to specify your first name!" );
		document.form1.firstname.focus();
		returnVal = false;
	}				
	else if ((document.form1.lastname.value).length == 0){
		alert("<br><br>You have to specify your last name!" );
		document.form1.lastname.focus();
		returnVal = false;
	}				
	else if ((document.form1.dateOfBirth.value).length == 0){
		alert("<br><br>You have to specify your date of birth!" );
		document.form1.dateOfBirth.focus();
		returnVal = false;
	}			
	else if ((document.form1.email.value).length == 0){
		alert("<br><br>You have to specify your email address!" );
		document.form1.email.focus();
		returnVal = false;
	}	
	else{
		returnVal = false;

		for (i=0;i<document.form1.share_detail.length;i++) {
			if (document.form1.share_detail[i].checked) {
				returnVal=true;
				break;
			}
		}		
		if (!returnVal ){
			alert("<br><br>Please indicate if you do want to share your details with a third party!" );		
		}

	}
	
	return returnVal;
}

/**
 * Forgotten password page
 */

function checkEmptyEmail(form1){
	var returnVal = true;
	
	if ((document.form1.transIdent.value).length == 0){
		alert("<br><br>You have to populate the Card Number!" );
		document.form1.transIdent.focus(); 
		returnVal = false;
	}
	
	return returnVal;
}





function checkReplacementCardEmpty(form1){
	if ((document.form1.name.value).length == 0){
		alert("<br><br>You have to enter a name to appear on the card" );
		document.form1.name.focus(); 
	}
	else {
		Dialog.confirm("<br>This will disable your current login/card. Are you sure that you want to continue?",{windowParameters: {width:287, height:185}, okLabel: "yes", cancelLabel: "no", cancel:function(win) {return false;},ok:function(win) {document.form1.submit();}});
	}
	return false;
}



function formsubmitEmail(form1){
	if (checkEmptyEmail(form1)){
		document.form1.submit();
	}
}

/**
 * Login page
 */
function checkEmpty(form1){
	var returnVal = true;				
	if ((document.form1.userID.value).length == 0){
		
		//alert("You have to populate the UserID!");
		alert("<BR><BR><br>You have to populate the UserID!");
		document.form1.userID.focus(); 
		returnVal = false;
	}
	else if ((document.form1.password.value).length == 0){
		alert("<br><br><br>You have to populate the Password!" );
		document.form1.password.focus();
		returnVal = false;
	}				
	return returnVal;
}

function formsubmit(form1){
	if (checkEmpty(form1)){
		document.form1.submit();
	}
}




/**
*	Transactions
*/
function formSubmitTrans(form1){
	 
	if(validateReportData()){
		document.form1.submit();
	}
}

function transactionDetails( path, transID) { 

	var url = path + '/servlet/SPTransactionServlet?transID='+transID+'&methodName=getTransDetails';
	window.open(url,'campaign','width=450,height=200,scrollbars=yes');
}


function validateIRD(ird)
{
	if (ird=="") return true;
	if (ird.length==8) ird="0"+ird;
	if (!(ird.length==9)) return false;
	// if (parseInt(ird)<=10000000) return false;  //this was mistake, int does not have enough digits to store 10000000
	var digit1 = parseInt(ird.charAt(0));
	var digit2 = parseInt(ird.charAt(1));
	var digit3 = parseInt(ird.charAt(2));
	var digit4 = parseInt(ird.charAt(3));
	var digit5 = parseInt(ird.charAt(4));
	var digit6 = parseInt(ird.charAt(5));
	var digit7 = parseInt(ird.charAt(6));
	var digit8 = parseInt(ird.charAt(7));
	var digit9 = parseInt(ird.charAt(8));
	if ( (digit1==0) && (digit2==0)) return false; 

	var sum = 3*digit1+2*digit2+7*digit3+6*digit4+5*digit5+4*digit6+3*digit7+2*digit8;

	var reminder = sum%11;
	if (reminder==1)
	{
		sum = 7*digit1+4*digit2+3*digit3+2*digit4+5*digit5+2*digit6+7*digit7+6*digit8;
		reminder = sum%11;
		if (reminder==1)
		{
			return false;
		}
	}

	if (!((11-reminder)%11 == digit9))
	{
		return false;
	}

	return true;
}




/**
 * Your details
 */
var changedDetails = false;
var ask_to_save = true;

function saveDetails(dobEntered){
	//ask_to_save = false;
	//alert(dobEntered);

	if((document.form1.FIRST_NAME.value).length == 0){
		alert('Please enter your First Name');
		return;
	}
	if((document.form1.LAST_NAME.value).length == 0){
		alert('Please enter your Last Name');
		return;
	}
	if((document.form1.GENDER_ID.value) == 3){
		alert('Please enter your Gender');
		return;
	}
	
	if(dobEntered){
		var dobDD 	= document.form1.dobDD.value;
		var dobMM 	= document.form1.dobMM.value;
		var dobYYYY = document.form1.dobYYYY.value;
		
		if(dobDD.length == 0 || dobMM.length == 0 || dobYYYY.length == 0){
			alert('Please enter your date of birth');
			return;
		}
		
		if(!validateDate(dobDD,dobMM,dobYYYY)) {
			alert('Please enter a valid date of birth');
			return;
		}
	}
	
	
	
	if((document.form1.PHYSICAL_ADDRESS_1.value).length == 0){
		alert('Please enter your Street');
		return;
	}
	
	
	if((document.form1.PHYSICAL_SUBURB.value).length == 0){
		alert('Please enter your Suburb');
		return;
	}
	
	if((document.form1.PHYSICAL_CITY.value).length == 0){
		alert('Please enter your City');
		return;
	}
	
	if((document.form1.TRADER_SOURCE_ID.value).length == 0){
		alert('Please enter your Region');
		return;
	}
	

	
	if((document.form1.MOBILE_NUMBER.value).length == 0){
		alert('Please enter your Mobile Number');
		return;
	}
	
	
	if((document.form1.NEW_PASSWORD.value).length > 0 && (document.form1.NEW_PASSWORD.value).length < 4){
		alert('Your new password must be at least 4 charectets long');
		return;
	}

	if((document.form1.NEW_PASSWORD.value).length > 0){
		
		if((document.form1.CONFIRM_PASSWORD.value).length == 0){
			alert('Please enter the new password confirmation');
			return;

		}

		if(!(document.form1.NEW_PASSWORD.value == document.form1.CONFIRM_PASSWORD.value)){
			alert('The password confirmation does not match the new password');
			return;
		}
	}
	//alert('**'+document.form1.MAIL_PREF3.checked);
	
	if(document.form1.MAIL_PREF3.checked == true && document.form1.EMAIL_ADDRESS.value.length ==0) {
			alert('Please enter your email');
			return;
	
	
	}
	
	
	
	
	
	
	document.form1.submit();
}



function saveReqDetails(){
 
	sDD		=document.form1.dobDD.value
	sMM		=document.form1.dobMM.value
	sYYYY	=document.form1.dobYYYY.value

	if((document.form1.VALIDATE1.value=="1") && (document.form1.STD1.value !="" && document.form1.PHONE_1.value =="")){
		alert("<br><br><br>Please enter a home phone number");
		return;
	}
	
	if((document.form1.VALIDATE2.value=="1") && (document.form1.STD2.value !="" && document.form1.PHONE_2.value =="")){
		alert("<br><br><br>Please enter a work phone number");
		return;
	}
	
	if((document.form1.VALIDATE3.value=="1") && (document.form1.MOBPREF.value!="" && document.form1.MOBILE_NUMBER.value =="")){
		alert("<br><br><br>Please enter a mobile number");
		return;
	}
	
	
	
	if((document.form1.VALIDATE1.value=="1") && (document.form1.STD1.value =="" && document.form1.PHONE_1.value !="")){
		alert("<br><br><br>Please enter STD for home phone number");
		return;
	}
	
	if((document.form1.VALIDATE2.value=="1") && (document.form1.STD2.value =="" && document.form1.PHONE_2.value !="")){
		alert("<br><br><br>Please enter STD for work phone number");
		return;
	}
	
	if((document.form1.VALIDATE3.value=="1") && (document.form1.MOBPREF.value =="" && document.form1.MOBILE_NUMBER.value !="")){
		alert("<br><br><br>Please enter STD for mobile number");
		return;
	}	
	
	
	if(validateDate(sDD,sMM,sYYYY)){
		document.form1.submit();
	}else{
		alert('<br><br><br>Date of birth is not valid');
	}
	

}


function saveDonations(cardNumber){

	if(validateDonationData(cardNumber)){
		document.form1.submit();
	}
	
}


function validateLogin(){
	if((document.form1.password.value).length == 0){
		alert('<br><br><br>Please enter your password');
		return;
	}
	if((document.form1.password.value) == '111'){
		alert('<br><br><br>Invalid Password');
		return;
	}
	
	document.form1.submit();
}

function activateCard(){

	if((document.form1.cardNumber.value).length == 0){
		alert('<br><br><br>Please enter your Card Number');
		return;
	}
	/*
	if((document.form1.password.value).length == 0){
		alert('<br><br><br>Please enter your Authorisation Code');
		return;
	}
	
	if((document.form1.emailAddress.value).length == 0){
		alert('<br><br><br>Please enter your Email Address');
		return;
	}
	
	if (!validateEmail(document.form1.emailAddress.value)){
		alert('<br><br><br>Email Address is not valid');
		return;

	}
	*/

	if((document.form1.newPassword.value).length < 6){
		alert('<br><br><br>New password must be 6 characters long');
		return;
	}
	
	var numberOfDigits=0;
	var numberOfAlphaCharacters=0;
	for (var i = 0; i < (document.form1.newPassword.value).length; i++){
   if ( (document.form1.newPassword.value).charAt(i)<='9' && (document.form1.newPassword.value).charAt(i)>='0')
					numberOfDigits++;
		else			
		   if  (
		   		((document.form1.newPassword.value).charAt(i)<='z' && (document.form1.newPassword.value).charAt(i)>='a')
		   		||
		   		((document.form1.newPassword.value).charAt(i)<='Z' && (document.form1.newPassword.value).charAt(i)>='A')
		   		)
		 		numberOfAlphaCharacters++;
			else {alert("<br><br><br>Password can contain only alphanumeric characters"); return;}
			 		
  }
    
   if (numberOfDigits<2) {
   		alert("<br><br><br>Password must contain at least 2 digits");
   		return;
   }
 
   if (numberOfAlphaCharacters<4) {
   		alert("<br><br><br>Password must contain at least 4 alphabetical characters");
   		return;
   }
   
   

	if((document.form1.newPassword.value) != (document.form1.newPasswordConfirm.value)   ){
		alert('<br><br><br>Confirmation password does not match the new password entered');
		return;
	}
	
	/*
	if((document.form1.passwordRemQuestion.value).length == 0){
		alert('<br><br><br>Please select a password reminder question');
		return;
	}
 
	if((document.form1.passwordRemAnswer.value).length == 0){
		alert('<br><br>Please enter the answer to the password reminder question you have selected');
		return;
	}*/

	document.form1.submit();

}

function activateCardAnswer(){

	if((document.form1.passwordAnswer.value).length == 0){
		alert('<br><br><br>Please enter your answer.');
		return;
	}
	document.form1.submit();

}

function changePassword(){

	if((document.form1.password.value).length == 0){
		alert('<br><br><br>Please enter your Old Password');
		return;
	}
	
	if((document.form1.newPassword.value).length < 6){
		alert('<br><br><br>New password must be 6 charecters long');
		return;
	}
	
	var numberOfDigits=0;
	var numberOfAlphaCharacters=0;
	for (var i = 0; i < (document.form1.newPassword.value).length; i++){
   if ( (document.form1.newPassword.value).charAt(i)<='9' && (document.form1.newPassword.value).charAt(i)>='0')
					numberOfDigits++;
		else			
		   if  (
		   		((document.form1.newPassword.value).charAt(i)<='z' && (document.form1.newPassword.value).charAt(i)>='a')
		   		||
		   		((document.form1.newPassword.value).charAt(i)<='Z' && (document.form1.newPassword.value).charAt(i)>='A')
		   		)
		 		numberOfAlphaCharacters++;
			else {alert("<br><br><br>Password can contain only alphanumeric characters"); return;}
			 		
  }
    
   if (numberOfDigits<2) {
   		alert("<br><br><br>Password must contain at least 2 digits");
   		return;
   }
 
   if (numberOfAlphaCharacters<4) {
   		alert("<br><br><br>Password must contain at least 4 alphabetical characters");
   		return;
   }
   
   

	if((document.form1.newPassword.value) != (document.form1.newPasswordConfirm.value)   ){
		alert('<br><br><br>Confirmation password does not match the new password');
		return;
	}
	
	document.form1.submit();

}



function resetDetails(){
	ask_to_save = false;
	document.form1.reset();
}

function setChanged(changed){
	changedDetails = changed;
}

function detectChanges(){
	if (ask_to_save){
		if(confirm("Do you want to save changes ? ")){
			document.form1.submit();	
		}
	}

}


function checkTechSupportEmpty(form1){
	 
	
/*	if ((document.form1.name.value).length == 0){
		alert("Please enter your name" );
		document.form1.name.focus(); 
		return false;
	}
*/
	
	if (document.form1.selectSubject.value == "Other"){
		if (((document.form1.subject.value).length == 0) || (document.form1.subject.value == "Enter your subject here")){
			alert("<br><br><br>Please enter your subject" );
			document.form1.subject.focus(); 
			return false;
		}
	}	

/*	
	if ((document.form1.contactMethod.value) != "Do Not Contact" &&  (document.form1.contactDetails.value).length == 0){
		alert("Please enter your Contact Details" );
		document.form1.contactDetails.focus(); 
		return false;
	}
*/	
	
	if ((document.form1.message.value).length == 0){
		alert("<br><br><br>Please enter your message" );
		document.form1.message.focus(); 
		return false;
	}
	
	
	return true;
}

function validateDate(day,month,year) {
		if (day < 29) return true;
		if (month == "1" | month == "01" |month == "3" | month == "03" | month == "5" | month == "05" | month == "7" | month == "07" |month == "8" | month == "08" | month == "10" | month == "12" ) 
				return true;
		if (month != "2") {
				if (day <= 30) 
					return true;
				else {
					return false;
				}
			}
			else {
				//check for leap year because it is Feruary
				if ( (1996-year)%4 == 0) {
					if (day <= 29) {
						return true;
					}
					else {
						return false;
					}
				}
				else {
					if (day <=28) {
						return true;		
					}
					else {
						return false;
					}
				}
			}
}


function validateReportData(){
	
	sDD		=document.form1.startDD.value
	sMM		=document.form1.startMM.value
	sYYYY	=document.form1.startYY.value

	eDD		=document.form1.endDD.value
	eMM		=document.form1.endMM.value
	eYYYY	=document.form1.endYY.value

	 

	result = true;

	if(!validateDate(sDD,sMM,sYYYY)){
		alert("<br><br><br>Start Date is invalid.");
		return false;
	}
	if(!validateDate(eDD,eMM,eYYYY)){
		alert("<br><br><br>End Date is invalid.");
		return false;
	}

	var startDate = new Date(sYYYY, parseInt(sMM,10)-1,sDD);
	var endDate   = new Date(eYYYY,parseInt(eMM,10)-1,eDD);

	if(startDate.getTime() > endDate.getTime()){
		alert("<br><br><br>Start Date cannot be after End Date");
		return false;
	}

	var today=new Date();

	if(startDate > today ){
		alert("<br><br><br>Start Date cannot be in the future");
		return false;
	}
	
	 

	return result;	
}


function validateDonationData(cardNumber){
	
	if ((document.form1.donRecepientLastName.value).length == 0){
		alert("<br><br>Please enter the Recepients Last Name" );
		document.form1.donRecepientLastName.focus(); 
		return false;
	}
	if ((document.form1.donRecepientLastName.value).length > 20){
		alert("<br><br>Recepient last name cannot be more than 20 characters long" );
		document.form1.donRecepientLastName.focus(); 
		return false;
	}

	if ((document.form1.donRecepientCard.value).length == 0){
		alert("<br><br><br>Please enter the Recepients Card Number" );
		document.form1.donRecepientCard.focus(); 
		return false;
	}
	
	
	
	if ((document.form1.donRecepientCard.value) == cardNumber){
		alert("<br><br><br>You cannot donate points to yourself" );
		document.form1.donRecepientCard.focus(); 
		return false;
	}
	
	if ((document.form1.donationPercentage.value).length == 0){
		alert("<br><br>Please enter the percentage amount you wish to donate to "+document.form1.donRecepientLastName.value );
		document.form1.donationPercentage.focus(); 
		return false;
	}

	if ((parseInt(document.form1.donationPercentage.value,10) <= 0) || (parseInt(document.form1.donationPercentage.value , 10) > 100)){
		alert("<br><br>Percentage amount must be greater than zero and less or equal 100");
		document.form1.donationPercentage.focus(); 
		return false;
	}



	return validateDonationDates();
}


function validateDonationDates(){
	
	sDD		=document.form1.startDD.value
	sMM		=document.form1.startMM.value
	sYYYY	=document.form1.startYY.value

	eDD		=document.form1.endDD.value
	eMM		=document.form1.endMM.value
	eYYYY	=document.form1.endYY.value

	 

	result = true;

	if(!validateDate(sDD,sMM,sYYYY)){
		alert("<br><br><br>Start Date is invalid.");
		return false;
	}
	
	if(eDD == "-1" || eMM == "-1" && eYYYY == "-1"){
		return result;
	}
	
	
	if(!validateDate(eDD,eMM,eYYYY)){
		alert("<br><br><br>End Date is invalid.");
		return false;
	}

	var startDate = new Date(sYYYY, parseInt(sMM,10)-1,sDD);
	var endDate   = new Date(eYYYY,parseInt(eMM,10)-1,eDD);

	if(startDate.getTime() > endDate.getTime()){
		alert("<br><br><br>Start Date cannot be after End Date");
		return false;
	}

 
	 

	return result;	
}

function confirmDonationCancellation(link){

	Dialog.confirm('<br><br>Are you sure you want to cancel selected Donation',{windowParameters: {width:287, height:185, background:"#FFF"}, okLabel: 'Yes', cancelLabel: 'No', cancel:function(win) {return false;},ok:function(win) {window.location.replace(link);return true;}});
	


}




