function upperFirst(value){
value=trim(value);
var len=value.length;
if(value!=""){
	return value.charAt(0).toUpperCase()+value.substring(1).toLowerCase();
}else{return value;}
}

function trim(value){
var temp=value;
while(temp.charAt(temp.length-1)==" "){
	if(temp.length>2){
		temp=temp.substring(0,temp.length-1);
	}else{temp="";}	
}
while(temp.charAt(0)==" "){
	if(temp.length>2){
		temp=temp.substring(1);
	}else{temp="";}	
}
return temp;
}

function keyCk(object){
	var keyascII;
	keyascII=window.event["keyCode"];
	if (keyascII==13){
		object.click();
	}
}

function upperFirstOnly(value){
var len=value.length;
if(len>0){
	var spacePosition, temp, newString;
	newString="";
	for(var i=0;i<len;i++){
		temp=value.charAt(i);
		if(i==0){
			if(value.charAt(i)!=" "){
				newString=newString+value.charAt(0).toUpperCase();
			}
			else{
				value=value.substring(1);
				return upperFirstOnly(trim(value));
			}
		}
		if(temp==" "&i<=len){
			while(value.charAt(i)==" "&i<=len){
				i=i+1;
			}
			if(value.charAt(i)!=" "){newString=newString+" "+value.charAt(i).toUpperCase();}
		}
		if(temp!=" "&i>0){newString=newString+value.charAt(i).toLowerCase();}
	}
	return trim(newString);
}
return trim(value);
}

function validateEmail(strEmail){
	var three = /[a-z,A-Z]/;		//regular expression used to check lower case
	var legalChars=/[a-z,A-Z,0-9,@,.,_,-]/;
	var len,bL1,bL2,len1
	len=strEmail.length;
	if(len<5){return false;}

	for(i=0;i<len;i++){if(!legalChars.test(strEmail.charAt(i)))return false;}

	bL1=false;
	bL2=false;
			
	len1=2;
	for (i=1;i<len-3;i++){
		if(strEmail.substring(i,i+1)=="@"){
			bL1=true;
			len1=i;
			break;
		}
	}

	for (i=len1+2;i<len-1;i++){
		if(strEmail.substring(i,i+1)=="."){
			bL2=true;
		}
	}

	if((strEmail.substring(0,1)=="@")|(strEmail.substring(len-1,len)=="@")){
		bL1=false;
	}

	if((strEmail.substring(0,1)==".")|(strEmail.substring(len1+1,len1+2)==".")|(strEmail.substring(len-1,len)==".")){
		bL2=false;
	}

	if ((bL1&bL2)==false){
		return false;
	}

	if(!three.test(strEmail.charAt(len-2))|!three.test(strEmail.charAt(len-1))){return false;}
	if((strEmail.charAt(len-4)!=".")&(strEmail.charAt(len-3)!=".")){return false;}

	for (i=0;i<len-1;i++){if(strEmail.charAt(i)=="."&strEmail.charAt(i+1)=="."){return false;}}

	return true;
}

function checkZipCode(countrySelectIndex,zipValue){	
	var three = /^[a-z]^[A-Z]/;		//regular expression used to check lower case
	//var frm=document.AddR_Form;
	var zip=zipValue;
	if(zip==""){
		window.alert("Zip Code can not be empty");
		return false;
	}

	if(zip !=""){
		if(countrySelectIndex==33){	
			//M4H 1J4 format						
			if(zip.length!=7){
				window.alert("Zip Code should be in this format (M4H 1J4)");
				return false;
			}
			if((zip.charAt(3)!=" ")|(parseInt(zip.charAt(0))).toString()==zip.charAt(0)|(parseInt(zip.charAt(1))).toString()!=zip.charAt(1)|				(parseInt(zip.charAt(2))).toString()==zip.charAt(2)|(parseInt(zip.charAt(4))).toString()!=zip.charAt(4)|(parseInt(zip.charAt(5))).toString()==zip.charAt(5)|(parseInt(zip.charAt(6))).toString()!=zip.charAt(6)){
				window.alert("Zip Code should be in this format (M4H 1J4)");
				return false;
			}
			if(three.test(zip.charAt(0))|three.test(zip.charAt(2))|three.test(zip.charAt(5))){
				window.alert("Zip Code should be in this format (M4H 1J4)");
				return false;
			}
		}
		if(countrySelectIndex==203){
			if(zip.length!= 5){
				window.alert("Zip Code should be in this format (14226)");
				return false;
			}
			if((parseInt(zip.charAt(0))).toString()!=zip.charAt(0)|(parseInt(zip.charAt(1))).toString()!=zip.charAt(1)|(parseInt(zip.charAt(2))).toString()!=zip.charAt(2)|(parseInt(zip.charAt(3))).toString()!=zip.charAt(3)|(parseInt(zip.charAt(4))).toString()!=zip.charAt(4)){
				window.alert("Zip Code should be in this format (14226)");
				return false;
			}
		}
	}	
	return true;
}

function validateDocFile(filename){
	var docFileExt=new Array(".doc",".txt",".rtf.",".pdf",".htm",".wps");
	var tempStr, len;
	len=filename.length;
	if(len>=5){
		tempStr=filename.substr(len-4,len-1).toLowerCase();
		for (var i=0;i<6;i++){
			if(docFileExt[i]==tempStr)
				return true;
		}
		return false;
	}
	else 
		return false;
}

function validateDate(dayValue,monValue,yearValue){
	if(parseInt(monValue)>12||parseInt(monValue)<1){
		window.alert("Month should be between 1 - 12.");
		return false;
	}
	if((monValue=="9"||monValue=="4"||monValue=="6"||monValue=="11")&&parseInt(dayValue)>30){
		window.alert("The month does not have more than 30 days.");
		return false;
	}
	if(parseInt(yearValue)%4!=0&&monValue=="2"&&parseInt(dayValue)>28){
		window.alert("The month does not have more than 28 days.");
		return false;
	}
	if(parseInt(yearValue)%4==0&&monValue=="2"&&parseInt(dayValue)>29){
		window.alert("The month does not have more than 29 days.");
		return false;
	}
	if(parseInt(dayValue)>31||parseInt(dayValue)<1){
		window.alert("The month has 1 - 31 days.");
		return false;
	}
	return true;			
}

function validateName(value){
	var three = /[a-z,A-Z]/;		//regular expression used to check lower case
	if(value){
		for(var i=0;i<value.length;i++){
			if(!three.test(value.charAt(i))){
				window.alert("The " + (i+1) + " character '"+value.charAt(i)+"' is incorrect in the value you have inputted");
				return false;
			}
		}
	}
}
function validateNameNoAlert(value){
	var three = /[a-z,A-Z,' ']/;		//regular expression used to check lower case
	if(value){
		for(var i=0;i<value.length;i++){
			if(!three.test(value.charAt(i))){
				//window.alert("Character " + (i+1) + " '"+value.charAt(i)+"' is not valid. Please use letters for this field.");
				return false;
			}
		}
	}
	return true;
}

function validateFullName(value){
	var three = /[a-z,A-Z]/;		//regular expression used to check lower case
	if(value){
		for(var i=0;i<value.length;i++){
			if(!three.test(value.charAt(i))){
				window.alert("The " + (i+1) + " character '"+value.charAt(i)+"' is incorrect in the value you have inputted");
				return false;
			}
		}
	}
}
function validateFullNameNoAlert(value){
	var three = /[a-z,A-Z,' ']/;		//regular expression used to check lower case
	if(value){
		for(var i=0;i<value.length;i++){
			if(!three.test(value.charAt(i))){
				//window.alert("Character " + (i+1) + " '"+value.charAt(i)+"' is not valid. Please use letters for this field.");
				return false;
			}
		}
	}
	return true;
}

function validatePartName(value){
	var three = /[a-z,A-Z]/;		//regular expression used to check lower case
	if(value){
		for(var i=0;i<value.length;i++){
			if(!three.test(value.charAt(i))){
				window.alert("The " + (i+1) + " character '"+value.charAt(i)+"' is incorrect in the value you have inputted");
				return false;
			}
		}
	}
}

function validatePartNameNoAlert(value){
	var three = /[a-z,A-Z]/;		//regular expression used to check lower case
	if(value){
		for(var i=0;i<value.length;i++){
			if(!three.test(value.charAt(i))){
				//window.alert("Character " + (i+1) + " '"+value.charAt(i)+"' is not valid. Please use letters for this field.");
				return false;
			}
		}
	}
	return true;
}

function validateDate1(dateValue){
var dayValue,monValue,yearValue;

if(dateValue.length!=""){
	if(dateValue.length<10){
		window.alert("Date Format is 'mm/dd/yyyy', check the value you input and try again.");
		return false;
	}
	if(dateValue.charAt(2)!="/"||dateValue.charAt(5)!="/"){
		window.alert("Date Format is 'mm/dd/yyyy', check the value you input and try again.");
		return false;
	}
	monValue=dateValue.substring(0,2);
	dayValue=dateValue.substring(3,5);
	yearValue=dateValue.substring(6,10);
	if(dayValue.charAt(0)=="0"){dayValue=dayValue.substring(1,2);}
	if(monValue.charAt(0)=="0"){monValue=monValue.substring(1,2);}
	if(parseInt(dayValue).toString()==dayValue&&parseInt(monValue).toString()==monValue&&parseInt(yearValue).toString()==yearValue){
		if(parseInt(monValue)>12||parseInt(monValue)<1){
			window.alert("Month should be between 1 - 12.");
			return false;
		}
		if((monValue=="9"||monValue=="4"||monValue=="6"||monValue=="11")&&parseInt(dayValue)>30){
			window.alert("The month does not have more than 30 days.");
			return false;
		}
		if(parseInt(yearValue)%4!=0&&monValue=="2"&&parseInt(dayValue)>28){
			window.alert("The month does not have more than 28 days.");
			return false;
		}
		if(parseInt(yearValue)%4==0&&monValue=="2"&&parseInt(dayValue)>29){
			window.alert("The month does not have more than 29 days.");
			return false;
		}
		if(parseInt(dayValue)>31||parseInt(dayValue)<1){
			window.alert("The month has 1 - 31 days.");
			return false;
		}
		return true;			
	}
	else{
		window.alert("Date Format is 'mm/dd/yyyy', check the value you input and try again.");
		return false;
	}
}
	return true;
}

