/**********************************************************************************
	File			:	CommonUtilities.js
	Description		:	This file is inclued in the asp pages and this contains 
						all the common utilities(functions) required all over the asp pages	
						ex:	Mandatorycheck etc
	Author			:	C04
**********************************************************************************/

//Function 1
//Author : C04
//Client side validations for the form
//isBlank function is used to check given String contains tab spaces or newline characters
// Called by CheckMandatory
function TrimSpecial(p_svin)
{
	l_str = "";
	for(var i=0;i< p_svin.length;i++)
	{
		var l_ctemp = p_svin.charAt(i);  //get a character
		if ((p_svin.charCodeAt(i) == 13 || p_svin.charCodeAt(i) == 10 || p_svin.charCodeAt(i) == 38 || p_svin.charCodeAt(i) == 60 || p_svin.charCodeAt(i) == 62 || p_svin.charCodeAt(i) == 34 || p_svin.charCodeAt(i) == 39 ) )  //check for tab and blank spaces
			
			{
				;
				
			}else
			{
				l_str = l_str + "" + l_ctemp + "";
			}	
					
	}
	
	return F_Trim(l_str);
}


//Function 2
// Check Mandatory function checks the mandatory fields of given form
// Called by the OnSubmit function ...
// Author : C04
//Input form name
// Logic comes here ...

function checkmandatory(p_oFrm)
{
	var l_smsg;
	var l_sEmptyFields="";
	var l_sErrors="";
	var l_ofirst;
	var l_bfirst = false;

	for(var i=0;i<p_oFrm.length;i++)   //for all the elements on the form
	{
		var l_oe = p_oFrm.elements[i];
		if(((l_oe.type == "text") || (l_oe.type=="textarea")) && !l_oe.optional)  //if textboxes and text areas mandatory
		{	
			if ((TrimSpecial(l_oe.value)==null)||(TrimSpecial(l_oe.value)==""))
			{
			
				if (!l_bfirst)
				{
					l_ofirst = l_oe;	//getting the first field which is mandatory
					l_bfirst = true;
				}
				l_sEmptyFields = l_sEmptyFields + " ;" + l_oe.name;
				continue;
			}
			if (l_oe.numeric && ((l_oe.min != "") || (l_oe.max != "")))	//for the fields with a max and min values
			{
				var l_val=TrimSpecial(l_oe.value);
				if ( isNaN(l_val) || 
						((l_oe.min != null) && (l_val <= l_oe.min)) ||
						((l_oe.max != null) && (l_val >= l_oe.max)))
				{
					if (!l_bfirst)		//get the first element not in the range
					{
						l_ofirst = l_oe;
						l_bfirst = true;
					}
					l_sErrors = l_sErrors + "-The Field " + l_oe.name +" must be a number";
								
					if (l_oe.min != "" || l_oe.min == 0)
						l_sErrors = l_sErrors + " that is greater than " + l_oe.min;
					if (l_oe.max != "" && l_oe.min != "")
						l_sErrors = l_sErrors + " and less than " + l_oe.max;
					else if (l_oe.max != "")
						l_sErrors = l_sErrors + " and less than " + l_oe.max;
					l_sErrors = l_sErrors + ".\n";
				}
			}
		}
	}
	if (l_sEmptyFields=="" && l_sErrors=="") return true;
	l_smsg="";
	if (l_sEmptyFields != "")
	{
		l_smsg = "- The following Mandatory field(s) are empty:" + l_sEmptyFields + "\n";
		if (l_sErrors != "") l_smsg += "\n";
	}
	l_smsg+=l_sErrors;
	alert(l_smsg);	//display the JavaScript error message
	l_ofirst.focus();	//Focus on the first Field in the above message
	return false;
}


//Function 3
//Function for Trimming  VIN Number entered
//Called by ValidateVIN function in e-roadassist_1.asp
//Author	:C04
//Login goes here.....

function F_Trim(p_svin)
{
	var l_leftp=0,l_rightp=0;
	for(var i=0;i<p_svin.length-1;i++)
	{	//remove all the blank spaces on the left od the string
		if (p_svin.charAt(i) != ' ')
		{
			l_leftp = i;
			break;
		}
	
	}
	for(i=p_svin.length-1;i>=0;i--)
	{	//remove all the blank spaces on the right of the string
		if (p_svin.charAt(i) != ' ')
		{
			l_rightp = i;
			break;
		}
		
	}
	return p_svin.slice(l_leftp,l_rightp+1);	//obtain the string with out blank spaces
}


//Function 4
//JavaScript Function for common error handling
//Author	:	C03
//The Login goes here........

function report_error(p_smsg,url,p_line)
{
	
	i_test = 10;
	o_win = window.open("","error" + c_error_count++,"width=500,height=280");
	o_doc = o_win.document;
	o_doc.write('<DIV align=center>');
	o_doc.write('<FONT SIZE=4 FACE="helvetica"><B>');
	o_doc.write('OOPS ..... a JavaScript error has occured !');
	o_doc.write('</B></FONT><BR><HR SIZE=4 WIDTH="80%">');
	o_doc.write('<FORM ACTION="mailto:'+ c_email + '" METHOD=post');
	o_doc.write('ENCTYPE="text/plain">');
	o_doc.write('<FONT SIZE=3>');
	o_doc.write('<I>Click the Report Error" button to send a bug report.</I><br>');
	o_doc.write('<INPUT TYPE="submit" VALUE="Report Error">&nbsp;&nbsp;');
	o_doc.write('<INPUT TYPE="button" VALUE="Dismiss" onClick="self.close()">');
	o_doc.write('</DIV><DIV align=right>');
	o_doc.write('<br>Your name<I>(optional)</I>:');
	o_doc.write('<INPUT SIZE=42 NAME="name" VALUE="">');
	o_doc.write('<br>Error Message : ');
	o_doc.write('<INPUT SIZE=42 NAME="message" VALUE="'+ p_smsg + '">');
	o_doc.write('<br>Document: <INPUT SIZE=42 NAME="url" VALUE="' + url +'">');
	o_doc.write('<BR>Line Number: <INPUT SIZE=42 NAME="line" VALUE="'+ p_line +'">');
	o_doc.write('<BR>Browser Version: ');
	o_doc.write('<INPUT SIZE=42 NAME="version" VALUE="'+navigator.userAgent + '">');
	o_doc.write('</DIV></FONT>');
	o_doc.write('</FORM>');
	o_doc.close();
	return true;
}

// to register the error handler
self.onerror = report_error;


//Function No:5
//Author	:C04
//Function for Checking length of the fields
//Logic goes here...
//this function is called onchange of fields and as form level validations also
function field_length(p_oField,p_fieldlength)
{
	if (p_oField.value.length > p_fieldlength)
	{	
		alert("Length of "+p_oField.name+" not to exceed "+p_fieldlength +" Characters");
		p_oField.focus();
		return false;
	}
	else
		return true;
}

//Function: 6
//JavaScript Function for Formatting Phone Number 
//is used in places user is required to enter phone number
//Author	: C04
//Logic goes Here
function validphone(p_oField)
{
	var strTemp="";
	for(i=0;i<p_oField.value.length;i++)
	{
		l_currChar = p_oField.value.substr(i,1);
		l_intCurrentChar = l_currChar.charCodeAt(0);
		if (48 <= l_intCurrentChar && l_intCurrentChar <=57)
		{
			strTemp = strTemp + l_currChar;
		}
	}
	strInput = strTemp;
	if (strInput.length == 11 && strInput.substr(0,1) == "1")
	{	strInput = strInput.substr(1,10);   }
	if (strInput.length != 10)
	{
		alert("Enter a valid 10 digit US phone Number");
		p_oField.focus();
		return false;
	}
	strTemp = "(";                             // "("
	strTemp = strTemp + strInput.substr(0,3);  // Area code
	strTemp1=strInput.substr(0,3);
	if (strTemp1=="000")
	{
		alert("Enter a valid 10 digit US phone Number");
		p_oField.focus();
		return false;
	}
	strTemp = strTemp + ") ";               //' ") "
	strTemp = strTemp + strInput.substr(3,3);    //' Exchange
	strTemp2=strInput.substr(3,3);    //' Exchange
	if (strTemp2=="000")
	{
		alert("Enter a valid 10 digit US phone Number");
		p_oField.focus();
		return false;
	}
	strTemp = strTemp + "-"                    //' "-"
	strTemp = strTemp + strInput.substr(6,9);     //' 4 digit part
	strTemp3=strInput.substr(6,9);     //' 4 digit part
	if (strTemp2=="0000")
	{
		alert("Enter a valid 10 digit US phone Number");
		p_oField.focus();
		return false;
	}
	p_oField.value = strTemp;
	return true;
}
