window.onerror = null;

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
/*
Class Names for the data types

required = Required fields
int = Integer data type
dbl = Double data type
email = Email data type
txt = String data type
date = Date data type
time = time data type
curr = currency data type
*/
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

/*~trim the string~*/
function trim(str) 
{
	while (str.charAt(0) == ' ')
		str = str.substring(1);
  	while (str.charAt(str.length - 1) == ' ')
		str = str.substring(0, str.length - 1);
  	return str;
}

/*~get object name~*/
function getObjName(obj)
{ 
     var sFldName = obj.getAttribute("objName");
     if (sFldName==null || sFldName=="")
         sFldName = "Field";
     return sFldName; 
}

/*~set focus to the control~*/
function SetFocus(obj)
{
	if (obj.type=='hidden' || obj.style.display=='none' || obj.style.visibility=='hidden') 
	{
		return;
	}
	else
	{
		if (obj.type=='text' || obj.type=='checkbox' || obj.type=='radio' || obj.type=='textarea' || obj.type=='select-one')
		{  
			if (obj.disabled == true)   
				return;
				
			if (obj.type=='text')   
				if (obj.readOnly)
					return; 
			
			obj.focus();       //else focusable and focus it.
			
			///code changed by shweta on 27jan2004
			if (obj.type !='select-one')
				obj.select();
		} 
	}
}


/*~form validation based on class name~*/
var arrFldType;
arrFldType=new Array();
arrFldType[0]='int';
arrFldType[1]='dbl';
arrFldType[2]='email';
arrFldType[3]='fax';
arrFldType[4]='date';
arrFldType[5]='time';
arrFldType[6]='curr';
arrFldType[7]='phone';

function validateForm(objFrm)
{
	
	var msg_vld_dbl = '';
	msg_vld_dbl = msg_vld_num;
	
	var retVal=objFrm.elements.length;
	for(var i=0;i<objFrm.elements.length;i++)
	{
		var elem=objFrm.elements[i];
		var elemClass=elem.className.toLowerCase();
		var elemValue=elem.value;
		
		var ifReq=false;
		
		if(elemClass!='')
		{
			if(elemClass.indexOf('required') != -1)
			{
				ifReq=true;
				//code changed by shweta on 27jan2004
				//OLD-------if((elem.type=='text'||elem.type=='textarea') && trim(elemValue).length == 0)
				//added-----|| elem.type=='select-one'
				//added code by shweta on 09April2004
				//elem.type=='password'
				if((elem.type=='text'||elem.type=='password'||elem.type=='textarea' || elem.type=='select-one') && trim(elemValue).length == 0)
				{
						//Added bys shweta to check commodity code
						if(getObjName(elem) == "Field")
							alert('Commodity Category ' + msg_vld_blank); 
						else	
							alert(getObjName(elem)+ ' ' + msg_vld_blank); 
						//MOD - kanwal - 16th July 2004 - shifted the cursor to first tab if the customer no is blank
						if(window.fnTabClick)
						{
							if(window.tabName)
							{
								var arrTmp = tabName.split(",")

								for (j=0; j < arrTmp.length; j++)
								{
									//alert(window.document.all[arrTmp[j]].innerHTML)
									if (window.document.all[arrTmp[j]].innerHTML.search("id=" + elem.name) > 0)	
									{	
										fnTabClick(j);
										elem.focus();	
									}
								}
							}

						}
						else
						{
								elem.focus();
						}	
							
						//MOD - kanwal - Upto here
						return i; //Return element index
				}	
				else if((elem.type=='select'||elem.type=='select-one') && elem.selectedIndex==-1)
				{
					alert(msg_vld_value);
					return i; //Return element index
				}
			}
			
			for(var j=0;j<arrFldType.length;j++)
			{
				var temtClass = arrFldType[j].toLowerCase();
				
				if(elemClass.indexOf(temtClass)>-1)
				{
				
					if(trim(elemValue).length == 0) break;
					if(!(eval("Is"+arrFldType[j]+"(elemValue)")))
					{
						alert(getObjName(elem) + " " + eval("msg_vld_"+arrFldType[j].toLowerCase()+""));
						//alert(getObjName(elem))
						
						return i;
					}
					break;
				}
			}
		}
	}

	return -1;
}	

/*real functions here*/

/*~check for the validity of currency type data~*/
function Iscurr(theInput,minrange,maxrange)
{
	msg_to_show = "";

	str = theInput;
	step = 0;
	count_dec = 0;
	error = false;
	allowed_chars = "+$,0123456789.()"
	parsestr = ""
	if(str.length == 0)
		return true; //in case there is no data then return default value 0 and exit
		
	for (i=0; i< str.length;i++)
	{
		count = 0; 
		if (count_dec > 1)	
			{
				error = true;	
				step = 3;
				break;
			}	//end if 
		
		if (str.charAt(i) == "-" && i!=0 )
			{
				step = 00;
				error = true;	
				break;
			}	
		if (str.charAt(i) == "+" && i!=0 )
			{
				step = 01;
				error = true;	
				break;
			}	
		
		if ((str.charAt(0) == "$" && str.charAt(1) == "$" ) || (str.charAt(i) == "$" && i>1))
			{
				step = 02;
				error = true;	
				break;
			}	
	
	
		if ( str.charAt(i) == "$" )
			{
				temp_valid = false;
				if (i == 0)
					temp_valid = true;
					
				if(i == 1 && (str.charAt(0) == "(" ||str.charAt(0) == "-"  || str.charAt(0) == "+"))
				   temp_valid = true;
				   
				if (i == 2 && (str.charAt(1) == "(" ) &&  (str.charAt(0) == "-"  || str.charAt(0) == "+"))
				   temp_valid = true;
				   
				if (temp_valid == false)
					{
						step = 02;
						error = true;	
						break;
					}	   
			}
// this was inserted to validate "()" in str, i.e. negative values
		if ( str.charAt(i) == "(" )
			{
				temp_valid = false;
				str_length = str.length ;
				
				if (i == 0 && str.charAt(str_length-1) == ")" )
					temp_valid = true;
				
				if (temp_valid == false)
				{
					step = 04;
					error = true;	
					break;
				}	   
			}
//***********End of Change
		
		for (j=0;j< allowed_chars.length; j++)//Checking that the chars entered match any of the allowed chars 
			{
				if (str.charAt(i) != allowed_chars.charAt(j))
					count++;
				else
					break;
			}//end for 
	
		if (count == allowed_chars.length)//-1)
			{
				error = true;	
				step = "-1"+ "	count= "+ count;
				break;
			}
			
			
		if (str.charAt(i)== ",")
			continue;
		
		if (str.charAt(i)== ".")
			{	
				count_dec++;
				continue;	
			}	
		if (count_dec > 1)	
			{
				error = true;	
				step = 3;
				break;
			}	//end if 
		
		if (arguments.length > 1)
			{
				if (str.charAt(i)!= "$" && str.charAt(i)!= "," && str.charAt(i)!= "(" && str.charAt(i)!= ")")
					parsestr += str.charAt(i);
			}		
	}//end for 

	if (arguments.length > 1)
		{
			parsestr = parseFloat(parsestr)
			minrange = parseFloat(minrange)
			maxrange = parseFloat(maxrange)
			if (parsestr < minrange || maxrange < parsestr || isNaN(parsestr))
				error = true;
		}		
		
	if (error == true)
		return false;
			
	return true;	
}//end Iscurr(obj)

/*~check if the validity of integer(num only no sign)~*/
function Isint(theInput)
{

	//inputValue = trim(inputValue);
	if (theInput != '') 
	{
		var theLength = theInput.length;
		for (var i = 0; i < theLength; i++)
		{
			var theChar = theInput.substring(i,i+1);
			if (theChar < "0" || theChar > "9")
			{
				if (i==0 && (theChar == "-" || theChar == "+"))
					i = 0//do nothing
				else	
					return false;
			}
		}
	}
	return true;
}
/*~check if the validity of real(num only no sign)~*/
function Isdbl(theInput)
{
	//inputValue =trim(inputValue);
	
	if (theInput != '') 
	{
		var theDecLoc = theInput.lastIndexOf(".");
		if ((theDecLoc != -1) && (theDecLoc != 0))
		{
			var theDecLocNext = theInput.lastIndexOf(".",theDecLoc - 1);
			if  (theDecLocNext != -1)
				return false; /*invalid if two dots*/
		}
		var theLength = theInput.length;
	for (var i = 0; i <= (theLength-1); i++)
		{
			var theChar = theInput.substring(i,i+1);

			if ((theChar == ".") && ((theLength == 1) || (i == theLength - 1)))
				return false; /*position of the dot*/
			if ((theChar < "0" || theChar > "9") && (theChar != "."))
				{
					
					if (i==0 && (theChar == "+" || theChar == "-"))
						i = 0//do nothing
					else	
						return false; /*other than 0-9 or .*/
				}
				
		}
	}	
	return true;
}

/*~check the validity of email id~*/
function Isemail(inputValue)
{
	var strEmail = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/; 
	//inputValue =trim(inputValue);
	if (inputValue != '') 
	{
		var matchArr = inputValue.match(strEmail);
	 	if(matchArr==null)
			return false;
	}
	return true;
}


/*~check the validity of email id~*/
function Iswebsite(inputValue)
{
	var strEmail = /^\w+((-\w+)|(\.\w+))*\.[A-Za-z0-9]+$/; 
	//inputValue =trim(inputValue);
	if (inputValue != '') 
	{
		var matchArr = inputValue.match(strEmail);
	 	if(matchArr==null)
			return false;
	}
	return true;
}

/*~no special character allowed~*/
function IsText(inputValue)
{
	//inputValue =trim(inputValue);
	if (inputValue != '') 
	for (var i=0;i<inputValue.length;i++)
	{
		var oneChar = inputValue.charAt(i);
		if ((oneChar >=" " && oneChar <="~" ))
			;
		else
			return false;
	}
	return true;
}
/*~check the validity of date for specified format~*/
function Isdate(inputValue)
{
	//inputValue = trim(inputValue)
	
	var sDateFormat="mm/dd/yyyy"; /* dd/mm/yy and dd/mm/yyyy*/
	var cSep = "/" /*default*/
	var arrDate 
	arrDate = new Array(); 
	
	//if(inputValue.length ==0)  return true;
	if(inputValue.length < 5) return false;
	//For years in the range '1970-1999'  should be entered as four char length strings 
	//For years in the range '2000-2099'  can be entered as 1-2 char length strings
 
	var mm=0, dd=1, yyyy=2
	var sDate, sMonth,sYear;
	if (arguments.length>1) 
	{
		sDateFormat=arguments[1];
		if (sDateFormat.toLowerCase()=='dd/mm/yyyy') 
		{
			dd=0; mm=1; yyyy=2; 
		}
	}			
	arrDate=inputValue.split(cSep);
    if(arrDate)
    {
		if (arrDate.length < 3 || arrDate.length > 3)
			return false;
    }
    else
		return false;
	if(arrDate[mm].length==0 || arrDate[dd].length==0 || arrDate[yyyy].length==0 || arrDate[mm].length>2 || arrDate[dd].length>2 || arrDate[yyyy].length>4)	
		return false;
	if (arrDate[mm]>12 || arrDate[mm]<1 || arrDate[dd]>31 || arrDate[dd]< 1 )
		return false;
	sMonth = parseInt(arrDate[mm],10);
    sDate = parseInt(arrDate[dd],10);
    sYear = parseInt(arrDate[yyyy],10);
    if(sYear>=0 && sYear<=99) sYear=sYear+ 2000;	
    if(sYear<1900||sYear>2099) return false;
    if(isNaN(sMonth) || isNaN(sDate) || isNaN(sYear)) return false;
    if ((sMonth == 4 || sMonth == 6 || sMonth == 9 || sMonth == 11 )&&(sDate > 30))
		return false;
	if ((sMonth == 1 || sMonth == 3 || sMonth == 5 || sMonth == 7 || sMonth == 8 || sMonth == 10 || sMonth == 12)&&(sDate > 31))
		return false;
	var by_4 = sYear%4; 
	var by_400 = sYear%400;
	var by_100 = sYear%100;
	var isLeap
    if (((by_4 == 0 && (by_100 == 0)))|| (by_400 == 0))
		isLeap = true;
	else
		isLeap = false;  
     
	if (!isLeap && sMonth == 2 && sDate > 28)
		return false;
 return true;  
}

/*~time validation in 12:39 am (hour:minute) format~*/
function Istime(inputValue)
{
	inputValue=trim(inputValue)
	if(inputValue=='') return true;
 	if (inputValue.indexOf(':') == -1 )
		return false;
    var hourminute=inputValue.split(':');
  
	if (hourminute.length > 2)
		return false;
	if (isNaN(hourminute[0]))
		return false;
	if (!( ( parseInt(hourminute[0],10)<=23) ) )//(parseInt(hourminute[0],10)>=1 ) && 
		return false;
	
	if (hourminute[1].indexOf(' ') == -1 )
		hourminute[1] += ' AM';

	var minutemeredium=hourminute[1].split(' ');	 
 	if (isNaN(minutemeredium[0]))
		return false;

	if (!( (parseInt(minutemeredium[0])>=0 ) && ( parseInt(minutemeredium[0])<=59) ) )
		return false;
 
	if ((minutemeredium[1].toUpperCase() != 'AM') && (minutemeredium[1].toUpperCase() != 'PM') )
		return false;
 return true;
}

function openzoomwindow(openurl,winwidth,winheight)
{
	var wtop = (screen.height - winheight) / 2
	var wleft = (screen.width - winwidth) / 2
	var att = "top=" + wtop + ",left=" + wleft + ",width=" + winwidth + ",height=" + winheight;
	window.open(openurl, '', att);
}


//function to Format Phone number ONKEYPRESS Added on 21 Feb 2004
function fn_PhoneTextChanged(obj)
{
	if (obj.value.length == 3)
		{
			obj.value += "/"
			return false;
		}	
		
	if (obj.value.length == 7)
		{
			obj.value += "-"
			return false;
		}	
	return true;	
}


//validate phone format
/*~check the validity of phone no.~*/
/*anything but blank space*/
function Isphone(theInput)
{
	//var strPhone1 = /^\d{3}[/b]\d{3}[-]\d{4}$/; //  ###/###-####
	//var strPhone2 = /^\d{3}[-]\d{3}[-]\d{4}$/; //  ###-###-####
	//var strPhone3 = /^\d{2}[" "]\d{1}[" "]\d{2}[" "]\d{2}[" "]\d{2}[" "]\d{2}$/; //  ## # ## ## ## ## PIS - 06/03/09 Format for france phone number 
	
	//var inputValue =trim(theInput);
	//if (inputValue != '') 
	//{		
	//	var matchArr = (inputValue.match(strPhone2) || inputValue.match(strPhone3));
	 //	if(matchArr==null)
	//		return false;
	//}
	return true;
}

function Isfax(theInput)
{
	
	//var strfax1 = /^\d{3}[/b]\d{3}[-]\d{4}$/; //  ###/###-####
	//var strfax2 = /^\d{3}[-]\d{3}[-]\d{4}$/; //  ###-###-####
	//var strfax3 = /^\d{2}[" "]\d{1}[" "]\d{2}[" "]\d{2}[" "]\d{2}[" "]\d{2}$/; //  ## # ## ## ## ## PIS - 06/03/09 Format for france fax number 
	
	//var inputValue =trim(theInput);
	//if (inputValue == "")
	//	return false;
	//if (inputValue != '') 
	//{
	//	var matchArr = (inputValue.match(strfax2) || inputValue.match(strfax3));
	// 	if(matchArr==null)
	//		return false;
	//}
	return true;
}


//validate phone format
/*function fn_CheckZipFormat(obj)
{
	if (IsZip(obj.value) == false)
		{
			alert(msg_valid_zip)
			obj.focus()
			return false;
		}	
	return true;
}

function IsZip(inputValue)
{
	var strZip1 = /^\d{5}[-]\d{4}$/; //  ###-###-####
	var strZip2 = /^\d{4}[-]\d{5}$/; //  ###-###-####
	
	var inputValue =trim(theInput);
	if (inputValue != '') 
	{
		var matchArr = (inputValue.match(strZip1) || inputValue.match(strZip2));
	 	if(matchArr==null)
			return false;
	}
	return true;
	
	/*inputValue =trim(inputValue);
	if (inputValue != '') 
	{
		for (var i=0;i<inputValue.length;i++)
		{
			var oneChar = inputValue.charAt(i);
	        if(oneChar == "-" && i == 0)
                return false;
			if (oneChar == "-"|| oneChar == " " ||(oneChar >="0" && oneChar <="9" ) || (oneChar >="A" && oneChar <="Z" ) || (oneChar >="a" && oneChar <="z" )) 
			;
			else
				return false;
		}	
	}
	return true;
}*/
