//Variables
var maxlength = 255;
var noerror="";


/**************************************************************************************************
Common Validations
**************************************************************************************************/
//1.Display Error Messages
function DisplayErrorMessages(errmsg){
	window.scrollTo(0,0);
	document.getElementById("errorfocus").focus();
	//Error
	if(errmsg) {
		errmsg = "The following information are required : <br><b>"+ errmsg+"</b>";
		document.getElementById("errormessages").style.display = "";
		document.getElementById("errormessages").innerHTML = errmsg;
		return false;
			
	} else {		
		document.getElementById("errormessages").innerHTML = "";
		document.getElementById("errormessages").style.display = "none";		
		return true;
	}	
	
}


//2.Trimming functions 
function trimAll(sString)
{
	while (sString.substring(0,1) == ' ') {
		sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == ' ') {
		sString = sString.substring(0,sString.length-1);
	}
	return sString;
}
function trimAllTextArea(sString)
{
	while (sString.substring(0,1) == '\n') {
		sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == '\n') {
		sString = sString.substring(0,sString.length-1);
	}
	return sString;
}

//3.Text Validations
function TextValidate(txtCtrl, errmsg)
{
	var textvalue = trimAll(txtCtrl.value);	
	if(textvalue =="") {
		return errmsg;		
	} else {
		if(textvalue.length > maxlength) {
			errmsg = "The Length of "+ errmsg +" should not be more than "+  errmsg +" characters!";	
			return errmsg;	
		}
	}
	return noerror;
}

//4.Text Validations
function TextAreaValidate(txtCtrl, errmsg)
{
	var textvalue = trimAll(trimAllTextArea(txtCtrl.value));	
	if(textvalue =="") {
		return errmsg;		
	} /*else {	
		if(textvalue.length > maxlength) {
			errmsg = "The Length of "+ errmsg +" should not be more than "+  val +" characters!";	
			return errmsg;	
		}
	}*/
	return noerror;
}

//5.Email validate
function EmailValidate(txtCtrl, errmsg) 
{
 	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	var emailvalue = trimAll(txtCtrl.value);
	if(emailvalue == "") {
		return errmsg;		
	}	else if (!filter.test(emailvalue)) 	{
		msg = "Please enter a valid email\t\n eg: username@domainname.com";
		return msg;
	}		
	return noerror;
}

//6.Date format validate
//To validate whether given date is in the form of dd/mm/yyyy
function DateFormatValidate(txtCtrl,errmsg)
{
	var datevalue = trimAll(txtCtrl.value);
	if(datevalue != "" && datevalue!= "MM/DD/YYYY")
	{		
		var strarr = datevalue.split("/");
		var m = strarr[0];
		var d = strarr[1];
		var y = strarr[2];
		var len = strarr.length;
		
		date =new Date();
		if(len > 3 || isNaN(d) || isNaN(m) || isNaN(y) || d<1 || d>31 || m<1 || m>12 || y<date.getFullYear()-40)
		{
			msg =  "Please enter valid "+errmsg;			
 			return msg;
		}
	}
	else
	{
 		return errmsg;	
	}
	return noerror;
}

function DefaultCtrlValue(el1, ctrlvalue){
 		el1.onfocus = function() {
			if (el1.value==ctrlvalue) 
				el1.value='';
		}
		el1.onblur = function() {
			if (el1.value=='') 
				el1.value=ctrlvalue;
		}
}

function OptionValidate(optCtrl,errmsg) 
{
		var itemchecked = false;
		//for(var j = 0 ; j < optCtrl.length ; ++j) 
		//{
			if(optCtrl.checked)
			{
			  itemchecked = true;
			 
			  //break;
			}
		//}
		if(!itemchecked) 
		{
			msg =  "Please choose an option for : "+errmsg ;
 			return msg;
		}
		return noerror;
} // Function End


function File_Extentsion_Validate_Doc(ctrl,errmsg)	
{  
	var fileuploadvalue = trimAll(ctrl.value);
	if(fileuploadvalue!= "")	
	{
		var strarr = fileuploadvalue.split(".");
		var len = strarr.length;
		if(len != 2)
		{
 			msg = "Please check file format !";
			return msg;
			
		}
		var type = strarr[1].toLowerCase();								
 		if (type!='doc' && type!='pdf' && type!='txt' && type!='docx') {							
			msg = "Please check file format !";
			return msg;
		}						
	} else if(fileuploadvalue == ""){ 
			return errmsg;
	}
	return noerror;
}


function NumberPriceValidate(txtCtrl, errmsg) 
{
	var numericvalue = trimAll(txtCtrl.value);
	if(isNaN(numericvalue)) 
	{
		msg = "Please enter numeric value for : " + errmsg + "";
 		return msg;
	} else if(numericvalue==""){ 
		return errmsg;	
	}
	return noerror;
}

/*
function validatePhone(txtCtrl,errmsg) {
    var error = "";
   // var stripped = txtCtrl.value.replace(/[\(\)\.\-\+\ ]/g, '');    
    if (txtCtrl.value == "") {
        error = "You didn't enter a "+errmsg+" number.\n";
       // txtCtrl.style.background = 'Yellow';
    } else if (isNaN((stripped))) {
        error = "The "+errmsg+" contains illegal characters.\n";
      //  txtCtrl.style.background = 'Yellow';
    } else if (stripped.length < 10 ) {
        error = "The "+errmsg+" is the wrong length. Make sure you included an area code.\n";
       // txtCtrl.style.background = 'Yellow';
    }
    return error;
}
*/

function validatePhone(txtCtrl,errmsg) { 
 
 var digits = txtCtrl.value.replace(/[^0-9]/ig, ''); 
 if (!digits) { 
 	error = "You didn't enter a "+errmsg+" number.\n";
	return error;
 } 
 if (isNaN((digits))) {
	error = "The "+errmsg+" contains illegal characters.\n";
  	return error;
 }
 if (digits.length == 10) { 
     phone.value = '(' + digits.substring(0, 3) + ') ' +  
     digits.substring(3, 6) + '-' +  
     digits.substring(6, 10); 
 } else { 
 	if (digits.length < 10 ) {
		error = "The "+errmsg+" is the wrong length. Make sure you included an area code.\n";
   		// txtCtrl.style.background = 'Yellow';
	} else {
  		 phone.value = digits; 
	}
 } 
 

	return error;
} 




/**************************************************************************************************
Form Validations
**************************************************************************************************/

//Validate Contact Us form
function Validate_Contact()
{
	var errmsg = new Array();
	//YourName
	errmsg[0] = TextValidate(document.doApp.ts_name,"Your Name");
 	//Subject
	errmsg[1] = TextValidate(document.doApp.ts_subject,"Subject");	
	//Subject
	errmsg[2] = TextAreaValidate(document.doApp.ta_comments,"Comments");
	
	
	var displaymsg ="";
	for(var i=0;i<errmsg.length;i++){
		if(errmsg[i].length){
 			if( displaymsg !=""){
				displaymsg = displaymsg+"<br>"+errmsg[i] ;
			} else {
				displaymsg = errmsg[i];
			}
		}
	}
 	return DisplayErrorMessages(displaymsg);
}

//Validate ENews form
function Validate_ENews()
{
	var errmsg = new Array();
	//YourName
	errmsg[0] = TextValidate(document.doApp.ts_name,"Your Name");
 	//Email
	errmsg[1] = EmailValidate(document.doApp.ts_email,"E-mail Address");	
 	
	
	var displaymsg ="";
	for(var i=0;i<errmsg.length;i++){
		if(errmsg[i].length){
 			if( displaymsg !=""){
				displaymsg = displaymsg+"<br>"+errmsg[i] ;
			} else {
				displaymsg = errmsg[i];
			}
		}
	}
 	return DisplayErrorMessages(displaymsg);
}



//Validate ENews form
function Validate_HippoTales()
{
	var errmsg = new Array();
	//YourName
	errmsg[0] = TextValidate(document.doApp.ts_childName,"Child's Name");
 	//Subject
	errmsg[1] = TextValidate(document.doApp.ts_address,"Address");	
 	
	
	var displaymsg ="";
	for(var i=0;i<errmsg.length;i++){
		if(errmsg[i].length){
 			if( displaymsg !=""){
				displaymsg = displaymsg+"<br>"+errmsg[i] ;
			} else {
				displaymsg = errmsg[i];
			}
		}
	}
 	return DisplayErrorMessages(displaymsg);
}



function Validate_Sponsorship(){
	
	var errmsg = new Array();
	//Date
	errmsg[0] = DateFormatValidate(document.doApp.da_date,"Date");
 	//Organization Name
	errmsg[1] = TextValidate(document.doApp.ts_organizationName,"Organization Name");
	//Organization Phone
	errmsg[2] = validatePhone(document.doApp.ts_organizationPhone,"Organization Phone");
 	//Contact Name
	errmsg[3] = TextValidate(document.doApp.ts_contactName,"Contact Name");
	//Contact Phone
	errmsg[4] = validatePhone(document.doApp.ts_contactPhone,"Contact Phone");
 	//Address
	errmsg[5] = TextValidate(document.doApp.ts_address,"Address");
 	//Email
	errmsg[6] = EmailValidate(document.doApp.ts_email,"E-mail");	
	//Organisztion size
	errmsg[7] = OptionValidate(document.doApp.to_organizationSize,"Is the organization?");	
	
	//Organisztion Type
	typeoptionerrmsg = OptionValidate(document.doApp.ts_organizationType,"What type of organization is it?");
	othertypemsg     = TextValidate(document.doApp.ts_organizationType2,"What type of organization is it?");
	if(typeoptionerrmsg=="" || othertypemsg=="" ) {
		errmsg[8] =	noerror;
	} else {
		errmsg[8] =	typeoptionerrmsg;
	}
	
	//Are you a member of the Finnish Credit Union? 
	errmsg[9] = OptionValidate(document.doApp.to_alreadyMember,"Are you a member of the Finnish Credit Union? ");
		
	//Who is affiliated with this group or event? 
	errmsg[10] = OptionValidate(document.doApp.to_whoAffiliated,"Who is affiliated with this group or event? ");
	
	//Organisztion size
	errmsg[11] = OptionValidate(document.doApp.to_publicRecognition,"Will Finnish Credit Union receive public to_publicRecognition?");
 	
	
	if(document.doApp.to_publicRecognition[0].checked==true){
		//If yes, in what way
		errmsg[12] = TextValidate(document.doApp.ta_whatWay,"If yes, in what way?");
	}
	
	//For what purposes will the donation be used? 
	errmsg[13] = TextAreaValidate(document.doApp.ta_donationPurpose,"For what purposes will the donation be used? ");
	
	
	//Advertising
	errmsg[14] = TextValidate(document.doApp.ts_advertising,"Advertising");
	
	//Cash (amount)
	errmsg[15] = NumberPriceValidate(document.doApp.ts_cash,"Cash (amount)");
	
	//Auction, raffle prize merchandise 
	errmsg[16] = TextValidate(document.doApp.ts_auction,"Auction, raffle prize merchandise ");
	
	//Other
	errmsg[17] = TextValidate(document.doApp.ts_other,"Other");
	
	//What are the other sources of funding for the event?
	errmsg[18] = TextValidate(document.doApp.ts_otherSources,"What are the other sources of funding for the event?");
	
	//What is the total budget? 
	errmsg[19] = TextValidate(document.doApp.ts_totalBudget,"What is the total budget? ");
	
	//Upload a document related to the event 
	errmsg[20] = File_Extentsion_Validate_Doc(document.doApp.ts_attachment,"Upload a document related to the event ");
		
	
	var displaymsg ="";
	for(var i=0;i<errmsg.length;i++){
		if(errmsg[i]){
			if(errmsg[i].length){
				if( displaymsg !=""){
					displaymsg = displaymsg+"<br>"+errmsg[i] ;
				} else {
					displaymsg = errmsg[i];
				}
			}
		}
	}
 	return DisplayErrorMessages(displaymsg);
}

//Validate Scholarship form
function Validate_Scholarship()
{
	var errmsg = new Array();
	//Surname
	errmsg[0] = TextValidate(document.doApp.ts_surname,"Surname");
 	//Membership#
	errmsg[1] = TextValidate(document.doApp.ts_memberID,"Membership#");	
	//Given names 
	errmsg[2] = TextValidate(document.doApp.ts_givenNames,"Given names");	
	//Address  
	errmsg[3] = TextValidate(document.doApp.ts_address,"Address");	
	
	//Telephone#
	errmsg[4] = validatePhone(document.doApp.ts_phone,"Telephone#");	
	
	//Email
	errmsg[5] = EmailValidate(document.doApp.ts_email,"Email");	
 	
	
	//Date and Place of Birth
	errmsg[6] = TextValidate(document.doApp.ts_dobPlace,"Date and Place of Birth");	
	
	
	//Institution Name  
	errmsg[7] = TextValidate(document.doApp.ts_institutionName,"Institution Name");	
	
	
	//Institution Address  
	errmsg[8] = TextValidate(document.doApp.ts_institutionAddress,"Institution Address");	
	
	
	// Field of Study / Study plan  
	errmsg[9] = TextAreaValidate(document.doApp.ta_fieldOfStudy," Field of Study / Study plan");
	
	
	//Volunteer work, hobbies, and extra-curricular programs  
	errmsg[10] = TextAreaValidate(document.doApp.ta_volunteer,"Volunteer work, hobbies, and extra-curricular programs");	
	
	//References
	/*********/
	
	if(document.doApp.ts_referenceName1.value=="" && document.doApp.ts_referenceName2.value=="" && document.doApp.ts_referenceName3.value==""){
		//Reference Name  
		errmsg[11] = TextAreaValidate(document.doApp.ts_referenceName1,"Reference Name");	
 	}
	
	if(document.doApp.ts_referenceAddress1.value=="" && document.doApp.ts_referenceAddress2.value=="" && document.doApp.ts_referenceAddress3.value==""){
		//Reference Address  
		errmsg[12] = TextAreaValidate(document.doApp.ts_referenceAddress1,"Reference Address");	
 	}
	
	
	if(document.doApp.ts_referencePhone1.value=="" && document.doApp.ts_referencePhone2.value=="" && document.doApp.ts_referencePhone3.value==""){
		//Reference Phone  
		errmsg[13] = validatePhone(document.doApp.ts_referencePhone1,"Reference Phone");	
 	}
	
	
	// What services do you currently use at Osuuspankki?
	errmsg[14] = TextAreaValidate(document.doApp.ta_currentServices,"What services do you currently use at Osuuspankki?");	
	
	//ta_futureServices  
	errmsg[15] = TextAreaValidate(document.doApp.ta_futureServices,"What services are you planning to use in the future?");	
	
	//Attach a Document to this Application:   
	errmsg[16] = File_Extentsion_Validate_Doc(document.doApp.ts_file,"Attach a Document to this Application");	
	
	
	//Date and Place   
	errmsg[17] = TextValidate(document.doApp.ts_dateAndPlace,"Date and Place ");	
	
	//Applicant’s Signature  
	errmsg[18] = TextValidate(document.doApp.ts_signature,"Applicant’s Signature ");	
	
	
	
	var displaymsg ="";
	for(var i=0;i<errmsg.length;i++){
		if(errmsg[i]){
			if(errmsg[i].length){
				if( displaymsg !=""){
					displaymsg = displaymsg+"<br>"+errmsg[i] ;
				} else {
					displaymsg = errmsg[i];
				}
			}
		}
	}
 	return DisplayErrorMessages(displaymsg);
}

function Validate_OnlineLoan2()
{

	var errmsg = new Array();
	//Surname
	errmsg[0] = TextValidate(document.loanApp.ts_amount,"Amount");
 	
	//alert("this is rweong"+errmsg[0]+"asdfsdf");
	//Membership#
	errmsg[1] = TextValidate(document.loanApp.ts_memberNumber,"Membership#");	
	//Given names 
	errmsg[2] = TextValidate(document.loanApp.ts_firstNameApplicant,"First Name Applicant");	
	//Address  
	errmsg[3] = TextValidate(document.loanApp.ts_lastNameApplicant,"Last Name Applicant");	
	
	//Telephone#
	errmsg[4] = TextValidate(document.loanApp.ts_telephoneApplicant,"Telephone#");	

	// Field of Study / Study plan  
	//errmsg[5] = TextAreaValidate(document.doApp.ta_fieldOfStudy," Field of Study / Study plan");
	
	
	
	//Email
	errmsg[5] = EmailValidate(document.loanApp.ts_yourEmail,"Your Email");
	
	errmsg[6] = OptionValidate(document.loanApp.cbox_agreement,"Hereby acknowledge notice from the Credit Union that a consumer report containing credit information is being or may be referred to in connection with the credit hereby applied for any renewal or extension thereof?");	
	
	var displaymsg ="";
	for(var i=0;i<errmsg.length;i++){
		if(errmsg[i]){
			if(errmsg[i].length){
				if( displaymsg !=""){
					displaymsg = displaymsg+"<br>"+errmsg[i] ;
				} else {
					displaymsg = errmsg[i];
				}
			}
		}
	}
 	return DisplayErrorMessages(displaymsg);
}

