//vilpesh- for sending history in mail

// Return true if value is an e-mail address
//not same as the one present in javascript.js
function isEmail(value) {
	//only commma can seperate all email addresses
	invalidChars = " /:;";
	if (value=="") return false;
	
	for (i=0; i<invalidChars.length;i++) {
	   badChar = invalidChars.charAt(i);
	   if (value.indexOf(badChar,0) != -1) return false;
	}
	
	atPos = value.indexOf("@", 1);
	if (atPos == -1) return false;
	if (value.indexOf("@", atPos + 1) != -1) return false;
	
	periodPos = value.indexOf(".", atPos);
	if (periodPos == -1) return false;
	
	if (periodPos+3 > value.length) return false;

	return true;
}

function isFieldBlank(theField) {
	inStr=trim(theField);
	if(inStr=="" || inStr == null)
		return true;
	else
		return false;
}

function trim(theField) {
	if(theField == "" || theField == null){
		return null;
	}
	inStr=theField.value;
	inStr=inStr.replace(' ', '');
	return inStr;
}

function validatetrackbyemailform(skytrackform)
 {
 	if(isFieldBlank(skytrackform.shipmentnumber0) && 
		isFieldBlank(skytrackform.shipmentnumber1) && 
		isFieldBlank(skytrackform.shipmentnumber2) && 
		isFieldBlank(skytrackform.shipmentnumber3) && 
		isFieldBlank(skytrackform.shipmentnumber4)) {
			if(isFieldBlank(skytrackform.toaddress))
			{
				alert("Please, Enter Shipment numbers and Mail Address(s).");
				skytrackform.shipmentnumber0.focus();
				return false;
			}
		alert("Please, Enter Shipment number(s).");
		skytrackform.shipmentnumber0.focus();
		return false;
	}
	//check if mail addresses are present
	if(isFieldBlank(skytrackform.toaddress))
	 {
		alert("Please, Enter Mail Address(s).");
		skytrackform.toaddress.focus();
		return false;
	 }

	//vilpesh starts to add from here 
	//MAX EMAIL ADDRESS TO BE ACCEPTED & PROCESSED - 10

	//DECLARATION----
	//enter mail address verifying loop only if 'entervloop' is true
	entervloop =  new Boolean(false);
	//maximum 10 mail addresses
	sepMailaddress = new Array(10);
	startpos = 0;
	//collect the mail address from the Textbox
	ma = skytrackform.toaddress.value;
	//alert(ma);
    
	//LOOP 1---------
	//get each mail address which are seperated by comma
	for (j=0;j<=9 ;j++ )
    {      
	  if (ma.indexOf(",", startpos) != -1)
      {
		//get the first position of ','
		endpos = ma.indexOf(",", startpos);
		//get the string upto the first occurence of ','
		sepMailaddress[j] = ma.substring(startpos, endpos);
        //fetch remaining string after ','
    	ma = ma.substring(endpos+1,ma.length);
		//alert(sepMailaddress[j]);		
		//if below variable is true then only validate the structure of email address
		//ie enter LOOP2
		entervloop = true;
      }
	  else
	  {
		  //found nothing after the last comma
		  break;
	  }
	}	 
	
	//here ma will contain remaining last email address
	// alert(ma);  

	//LOOP2--------
	 //get the value of (j-1) which is the total email addresses
	 k = j - 1;
	 j = 0;
	if (entervloop == true)
	{	
	  //verifying loop: check if valid looking email addresses are present
	  while(j != k)
	  {
	 	//alert(sepMailaddress[j]);
		if(isEmail(sepMailaddress[j]) == false) 
		{ 
		    alert ("Invalid mail address.");
			return false; 
		}
		else
		{
		   //the last mail address gets stored in 'ma' and not in sepMailaddress[]
		   if (isEmail(ma) == false)
		   { 
		     alert ("Invalid mail address..");
			 return false;
		   }
		}
		j++;
	  } //end of while       
	} //end of if
	else
	{
		//the only address present,check if it is valid
		if (ma.length <= 0 || isEmail(ma) == false)
		{ 
		     alert ("Invalid mail address...");
			 return false;
		}		
	}
	//added by vilpesh uptil here

 	var xState = skytrackform.cbTandC.checked;
 	if( !xState )
 	{
 		alert("Please, accept Terms and Conditions to view the result.");
 		return false;
 	}
 	return true;
 }
//Function Added by Jagan to open a customized window for view
//this function called in skytrack.jsp

function openWindow(mylink, windowname)
{
if (! window.focus)return true;
var href;
if (typeof(mylink) == 'string')
   href=mylink;
else
   href=mylink.href;
window.open(href, windowname, 'status=yes,width=400,height=200,scrollbars=yes');
return false;
}

//function added for jagan's Skypod
function open_Window(mylink, windowname)
{
if (! window.focus)return true;
var href;
if (typeof(mylink) == 'string')
   href=mylink;
else
   href=mylink.href;
window.open(href, windowname, 'status=yes,width=360,height=550,scrollbars=Yes');
return false;
}
 









 