//----------------------------------------------------------------------------------------
// verify screen form fields
//----------------------------------------------------------------------------------------
function verify(sForm) 
{
	var frm=document.forms[sForm];
	var doVerify = false;
	var msg;
	var empty_fields = "";
	var errors = "";

	//initialise form fields on required screens
	if (sForm == "MobilePhoneLeadsReport") 
	{
		frm.FromDate.optional = false;		
		frm.ToDate.optional = false;
		
		MakeControlNonOptionalIfValueGiven(frm.FromDate);
		MakeControlNonOptionalIfValueGiven(frm.ToDate);		
		
		doVerify = true;
	}

	if (sForm == "ccAgentCapture") 
	{
		frm.cciccenumber.optional = true;
		doVerify = true;
		
		if (frm.ccmediacode.selectedIndex == 0)
		{
			errors += "You must select a Media Code";
//			  empty_fields += "\n\  Media Code";
		}
	}
	
	if (sForm == "affiliateSignUp") 
	{
		doVerify = true;
	}
	
	if (sForm == "PrizeDrawSignUp") 
	{
		doVerify = true;
	}	
	
	if (sForm == "inviteFriends") 
	{
		frm.name1.optional = true;
		frm.email1.optional = true;
		frm.name2.optional = true;
		frm.email2.optional = true;
		frm.name3.optional = true;
		frm.email3.optional = true;
		frm.name4.optional = true;
		frm.email4.optional = true;
		frm.name5.optional = true;
		frm.email5.optional = true;
		
		if ((frm.name1.value == '') || (frm.email1.value == ''))
		{
			alert('You must enter at least one friends details.');				
			return false;
		}
		
		doVerify = true;
	}


	if (sForm == "miniSiteDetail") 
	{
		frm.Parameter.optional = true;
		frm.ReferId.optional = true;
		frm.HomePageText.optional = true;
		frm.LogoUrl.optional = true;
		frm.LogoLinkUrl.optional = true;
		frm.CalculatorEnergyDualPage.optional = true;
		frm.CalculatorEnergyGasPage.optional = true;		
		frm.CalculatorEnergyElectricPage.optional = true;		
		frm.CalculatorEnergyElectricPage.optional = true;		
		frm.CalculatorHomePhonePage.optional = true;		
		frm.CalculatorCreditCardPage.optional = true;		
		frm.CalculatorBroadbandPage.optional = true;		
		frm.HeaderIFrameUrl.optional = true;		
		frm.headerhtml.optional = true;				
		frm.HeaderBannerHTML.optional = true;		
		frm.aboutustext.optional = true;		
		frm.ContactName.optional = true;		
		frm.ContactEmailAddress.optional = true;		
		frm.UserName.optional = true;
		frm.Password.optional = true;
		frm.switchingguidepromohtml.optional = true;
		frm.telephonenumber.optional = true;		
		frm.showsslogo.optional = true;
		frm.showrightlayoutcolumn.optional = true;
		frm.mobilephoneenabled.optional = true;
		frm.calculatormobilephonepage.optional = true;
		frm.homepageurl.optional = true;
		frm.cashbackhtml.optional = true;
		frm.additionalsidebaritemHTML.optional = true;
		frm.HeadInc.optional = true;
		frm.showphonenumber.optional = true;
		frm.switchedothertext.optional = true;

		
		doVerify = true;
	}
	
	if (sForm == "affiliateDetail") 
	{
		frm.contactName.optional = false;		
		frm.emailAddress.optional = false;
		
		doVerify = true;
	}
	
	if (sForm == "affiliateChangePassword") 
	{
		frm.newPassword1.optional = false;
		frm.newPassword2.optional = false;	
		
		// check the passwords match
		if ((frm.newPassword1.value != '') && (frm.newPassword2.value !=''))
		{
			if (frm.newPassword1.value != frm.newPassword2.value)
			{	
				errors += "The new passwords do not match";
				doVerify = true;
			}
		}
	
		doVerify = true;
	}
	
	if (sForm == "ReferralSummaryReport") 
	{
		frm.FromDate.optional = true;		
		frm.ToDate.optional = true;
		
		MakeControlNonOptionalIfValueGiven(frm.FromDate);
		MakeControlNonOptionalIfValueGiven(frm.ToDate);		
		
		doVerify = true;
	}
	
	if (sForm == "ReferralDetailReport") 
	{
		frm.FromDate.optional = false;		
		frm.ToDate.optional = false;
		
		MakeControlNonOptionalIfValueGiven(frm.FromDate);
		MakeControlNonOptionalIfValueGiven(frm.ToDate);		
		
		doVerify = true;
	}
	
	if (sForm == "PageImpressionsReport") 
	{
		doVerify = true;
	}

	if (sForm == "VisitorsReport") 
	{
		frm.FromDate.optional = false;		
		frm.ToDate.optional = false;
		
		MakeControlNonOptionalIfValueGiven(frm.FromDate);
		MakeControlNonOptionalIfValueGiven(frm.ToDate);		
		
		doVerify = true;
	}
	
	if (sForm == "VisitorsReportByHour") 
	{
		frm.FromDate.optional = false;		
		frm.ToDate.optional = false;
		
		MakeControlNonOptionalIfValueGiven(frm.FromDate);
		MakeControlNonOptionalIfValueGiven(frm.ToDate);		
		
		doVerify = true;
	}

	if (sForm == "EntryPageReport") 
	{
		doVerify = true;
	}
	
	if (sForm == "ExitPageReport") 
	{
		doVerify = true;
	}
	
	if (sForm == "ConversionReport") 
	{
		doVerify = true;
	}
	
	if (sForm == "newsStoryDetail") 
	{
		frm.keywords.optional = true;
		frm.stoppress.optional = true;
		
		doVerify = true;
	}

	if (sForm == "ReferringSitesReport") 
	{
		doVerify = true;
	}
	
	if (sForm == "newsletterSignUp") 
	{
		doVerify = true;
	}
	
	//YB 1minus1 05/06/2007 (Ticket:CZR-862065)
	if (sForm == "newsletterSignUp1") 
	{
		doVerify = true;
	}	
	
	if (sForm == "EnergyPreCalc") 
	{
		frm.p.optional = false;
		
		doVerify = true;		
	}

	if (sForm == "customerCapture") 
	{
		// check for confirmed email address
		var email =  frm.emailaddress.value;
		var emailconfirmed = frm.emailaddressconfirm.value;		
		
		if ((email) && (emailconfirmed))
		{
				if ((email) != (emailconfirmed))
				{
					errors += "The email addresses do not match.\n";
				}
		}
		
		doVerify = true;		
	}
	
	if (sForm == "ChannelByMonthReport") 
	{
		var dates = frm.document.getElementById("dates");
		
		if (dates.selectedIndex == -1)
		{
			errors += "You must select at least one month.\n";
		}

		doVerify = true;		
	}
	
	//only check initialised screens - see above
	if (doVerify) 
	{
		for(var i = 0; i < frm.length; i++) 
		{
			var e = frm.elements[i];

			if (e.type == "textarea") 
			{
				//check the maxlength
				if (e.value.length > e.maxlength) {
//					e.value = e.value.substring(0,(e.maxlength))
					errors += "- " + e.id + " cannot be longer than " + e.maxlength + " characters.\n";
				}
			}

			if (((e.type == "text") || (e.type == "textarea")) && !e.optional) 
			{
				
				// first check if the field is empty
				if ((e.value == null) || (e.value == "")) 
				{
					empty_fields += "\n  " + e.id;
					continue;
				}

				// check if its date
				if ((e.id.indexOf("date") > -1) || (e.id.indexOf("Date") > -1)) 
				{

					if (!isDate(e.value, 0))
						errors += "- " + e.id + " is an invalid date format (dd/mm/yyyy).\n";
				}

			}
			
			if ((e.type == "password") && !e.optional) {
				// first check if the field is empty
				if ((e.value == null) || (e.value == "")) {
					empty_fields += "\n      " + e.id;
				}
				//then check for minimum length
/*				if ((e.value.length < 5) && !(e.id == "CurrentPassword")) {
					errors += "- " + e.id + " must be at least 5 characters.\n";
				}*/
				continue;
			}

			if ((e.type == "password") && e.optional) {
				// for check if the field is not empty
				if ((e.value != null) && (e.value != "")) {
					//then check for minimum length
/*					if (e.value.length < 5) {
						errors += "- " + e.id + " must be at least 6 characters.\n";
					}*/
				}
				continue;
			}

			if (e.numeric) {
				var v = parseFloat(e.value);
				if (isNaN(v)) 
					errors += "- " + e.id + " must be a number.\n";
			}

		}
		
		msg = '';		
	
		// now, if there were any errors, display error messages and return false
		//otherwise return true
		if (!empty_fields && !errors) return true;
	
		//msg = "The form was not submitted because of the following error(s).\n";
		//msg += "Please correct these error(s) and re-submit.\n";
		//msg += "________________________________________________\n\n";
		
		if (empty_fields) {
			msg += "The following required fields are empty:"
						+ empty_fields + "\n"
			if (errors) msg += "\n";
		}
		msg += errors;
		alert(msg);
		return false;
	}
	else {
		return true;
	}
}

/**********************************************************************/ 
/*Function name :isDigit(theDigit) */ 
/*Usage of this function :test for an digit */ 
/*Input parameter required:thedata=string for test whether is digit */ 
/*Return value :if is digit,return true */ 
/* else return false */ 
/**********************************************************************/ 
function isDigit(theDigit) 
{ 
	var digitArray = new Array('0','1','2','3','4','5','6','7','8','9'),j; 

	for (j = 0; j < digitArray.length; j++) 
		{if (theDigit == digitArray[j]) 
			return true 
		} 
			return false 

} 

/*************************************************************************/ 
/*Function name :isPositiveInteger(theString) */ 
/*Usage of this function :test for an +ve integer */ 
/*Input parameter required:thedata=string for test whether is +ve integer*/ 
/*Return value :if is +ve integer,return true */ 
/* else return false */ 
/*function require :isDigit */ 
/*************************************************************************/ 
function isPositiveInteger(theString) 
{ 
	var theData = new String(theString) 

	if (!isDigit(theData.charAt(0))) 
		if (!(theData.charAt(0)== '+')) 
			return false 

	for (var i = 1; i < theData.length; i++) 
		if (!isDigit(theData.charAt(i))) 
			return false 

	return true 
} 

/**********************************************************************/ 
/*Function name :isDate(s,f) */ 
/*Usage of this function :To check s is a valid format */ 
/*Input parameter required:s=input string */ 
/* f=input string format */ 
/* =1,in mm/dd/yyyy format */ 
/* else in dd/mm/yyyy */ 
/*Return value :if is a valid date return 1 */ 
/* else return 0 */ 
/*Function required :isPositiveInteger() */ 
/**********************************************************************/ 
function isDate(s,f) 
{
	var a1=s.split("/"); 
	var a2=s.split("-"); 
	var e=true; 
	
	if ((a1.length!=3) && (a2.length!=3)) 
	{ 
		e=false; 
	} 
	else 
		{if (a1.length==3) 
		
	var na=a1; 
	if (a2.length==3) 
	var na=a2; 
	if (isPositiveInteger(na[0]) && isPositiveInteger(na[1]) && isPositiveInteger(na[2])) 
	{ if (f==1) 
{var d=na[1],m=na[0]; 
} 
else 
{var d=na[0],m=na[1]; 
} 
var y=na[2]; 
if (((e) && (y<1000)||y.length>4)) 
e=false 
if (e) 
{ 
v=new Date(m+"/"+d+"/"+y); 
if (v.getMonth()!=m-1) 
e=false; 
} 
} 
else 
{ 
e=false; 
} 
} 
return e 
} 

function MakeControlNonOptionalIfValueGiven(control)
{
		if ((control.value != "")) 
		{
				control.optional = false;
		}
}