// javaScript library

/*
//Works only for IE
//Disable Right Click of Content Frame
function disableRtClick()
{

window.event.returnValue=false

}
//include the below link on the <body> tag
//oncontextmenu="disableRtClick()" onselectstart="disableRtClick()"
*/

//Required Field validation
function textRequired(thisObj, fieldName) {
	if (thisObj.value.length == "") {
		if (document.trafficForm.sLanguage.value == "S"){
			alert(fieldName + " Se requieren.");
		}else{
			alert(fieldName + " is required.");
		}
		thisObj.focus();
		return false;
	}
	if (isBlank(thisObj.value)) {
		
		if (document.trafficForm.sLanguage.value == "S"){
			alert(fieldName + " Se requieren.");
		}else{
			alert(fieldName + " is required.");
		}
		thisObj.focus();
		return false;
	}
		
	return true;
}

//Checks to See if user only entered blanks
function isBlank(strValue) 
{
	var intIndex;
	var boolBlank;
	boolBlank = true;
	if (strValue.length != 0)
	{
		for (intIndex = 0; intIndex <  strValue.length; intIndex ++)
		{
			if (strValue.charAt(intIndex) != " ")
			{
				boolBlank = false;
				break;
			}
		}
	}
	return boolBlank;
}		


//Validate Selection of DropDown Required
function dropDownSelectionRequired(formField,fieldLabel)
{
  var result = true;
  
  if (formField.value == "-1")
  {
    	if (document.trafficForm.sLanguage.value == "S"){
		alert("Debe seleccionar un(a) " + fieldLabel + ".");
	}else{
		alert("You must select a " + fieldLabel + ".");
	}
	
    formField.focus();
    result = false;
  }
  
  return result;
}


//Validate Radio Button Selection Required
function radioSelectionRequired(thisObj, fieldName) {
	
	//If single radio display it wont enter the loop
	for ( x=0; x < thisObj.length; x++) {
		
		if (thisObj[x].checked == true) {
			return true;
		}
	}

	//Single radio display check
	//As single display its no longer an array
	if(thisObj.checked == true){
		return true;
	}

	if (document.trafficForm.sLanguage.value == "S"){
		alert("Debe seleccionar un(a) " + fieldName + ".");
	}else{
		alert("You must select a " + fieldName + ".");
	}
	
	return false;
}

//Date Validation - In MM/DD/YYYY format
function validDate(formField,fieldLabel,required)
{
  var result = true;
  var daysInMonth = DaysArray(12);
  if (required && !textRequired(formField,fieldLabel))
    result = false;
  
   if (result)
   {
     var elems = formField.value.split("/");
     
     result = (elems.length == 3); // should be three components
     
	 //Check for format
     if (result)
     {
       var month = parseInt(elems[0],10);
        var day = parseInt(elems[1],10);
       var year = parseInt(elems[2],10);
      result = !isNaN(month) && (month > 0) && (month < 13)  &&
            !isNaN(day) && (day > 0) && (day < 32)  &&
            !isNaN(year) && (elems[2].length == 4);
			
			//Check for month and day specific format
	 		if ( (result) && ((month==2 && day>daysInFebruary(year)) || day > daysInMonth[month] )){
				//alert("Please enter a valid day");
				result = false;
			}
     }
    
      if (!result)
     {
       
	if (document.trafficForm.sLanguage.value == "S"){
//		alert(fieldLabel +" Su fecha debe ser en el formato mm/dd/aaaa."  );
		alert(fieldLabel +" es inválida."  );
	}else{
	//	alert(fieldLabel + " must be in mm/dd/yyyy format."  );
		alert(fieldLabel + " is invalid."  );
	}
		
      	formField.focus();    
    }
  } 
  
  return result;
}

//Check for Leap Year
function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
//Check for Number of Days in a Month
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this;
}

//Checks for credit card beginning sequence
function validateCCTypeCCNum(ccnum, cctype)
{
	/* cctype is index of cctype drop down list
		1 = Discover
		2 = Mastercard
		3 = Visa
	*/
	
	if(cctype == "1")
	{
		lowerbound = 601100;
		upperbound = 601199;
	}
	else if(cctype == "2")
	{
		lowerbound = 510000;
		upperbound = 559999;
	}
	else if(cctype == "3")
	{
		lowerbound = 400000;
		upperbound = 499999;
	}
	else
	{
		upperbound = 0;
		lowerbound = 0;
	}
	
	firstsix = ccnum.substring(0, 6);
	
	if ((firstsix > upperbound) || (firstsix < lowerbound))
	{
		
		if (document.trafficForm.sLanguage.value == "S"){
			alert("Su tarjeta de crédito no es válido.");
		}else{
			alert("Invalid Credit Card Number.");
		}
		return false;
	}
	else
	{
		return true;
	}
}

//Check credit card length
function checkCreditCardLength(ccnumlength,cctypeindex){
	/* cctype is index of cctype drop down list
		1 = Discover
		2 = Mastercard
		3 = Visa
		Visa can be of 16 or 13 length while master and discover are 16
	*/
	if((ccnumlength != 16)&&(ccnumlength != 13))
	{
		if(((ccnumlength != 16)&&(ccnumlength != 13)) && (cctypeindex == "3"))
		{
			
			if (document.trafficForm.sLanguage.value == "S"){
				alert("Falta parte del número de tarjeta de crédito .");
			}else{
				alert("Credit card number is of the wrong length.");
			}
			return false;
		}
		if(cctypeindex != "3")
		{
			if (document.trafficForm.sLanguage.value == "S"){
				alert("Falta parte del número de tarjeta de crédito .");
			}else{
				alert("Credit card number is of the wrong length.");
			}
			return false;
		}
		
	}
	return true;
}

//checks credit card expiration
function creditCardExpiration(thisMonth,thisYear){
	var time=new Date();	
	currYear = time.getYear();
	currMonth = time.getMonth() + 1;
	selectedMonth = thisMonth.selectedIndex;
	selectedYear = currYear + (thisYear.selectedIndex - 1);
	selectedYear = String(selectedYear).substring(String(selectedYear).length -2, String(selectedYear).length);
	currYear = String(currYear).substring(String(currYear).length - 2, String(currYear).length);

	if(selectedYear < currYear)
	{
	
		if (document.trafficForm.sLanguage.value == "S"){
			alert("Su tarjeta de crédito está vencida.");
		}else{
			alert("Your credit card has expired.");
		}
		thisYear.focus();
		return false;
	}
		
	if((selectedYear == currYear))
	{
		if(selectedMonth < (currMonth))
		{
			if (document.trafficForm.sLanguage.value == "S"){
				alert("Su tarjeta de crédito está vencida.");
			}else{
				alert("Your credit card has expired.");
			}
			thisMonth.focus();
			return false;
		}
	}
	return true;

}

//Mod 10 Check for credit card number
function isvalidCCNumber(strCCNumber) 
{
	var k, multiplyFlag
	var TCCNumber
	var checksum
	var chkdigit, chktotal
	var validCC
	TCCNumber = "";
	validCC=true; 
	multiplyFlag=true;
	checksum=0;
	chkdigit=0;
	chktotal=0;
	// strip out anything that isn't a digit
	for (k=0;k<strCCNumber.length;k++)
	{
		if (!(strCCNumber.charAt(k) < '0' || strCCNumber.charAt(k) > '9'))
		{
			TCCNumber=TCCNumber+strCCNumber.charAt(k);
		}
	}
	// all the junk characters are gone
	// see if it's blank
	if (isBlank(TCCNumber))
	{
		validCC = false;
	}
	if (validCC && TCCNumber.length>2)
	{
	// validate checksum
		checksum=parseInt(TCCNumber.charAt(TCCNumber.length-1),10);
		for (k=(TCCNumber.length-2);k>=0;k--)
		{
			chkdigit = parseInt(TCCNumber.charAt(k),10);
			if (multiplyFlag)
			{
						chkdigit*=2;
			}
			multiplyFlag = !multiplyFlag;
			chktotal += Math.floor(chkdigit/10)+chkdigit%10;
		}
		if ((((chktotal%10)+checksum)%10)!=0)	
		{
			validCC = false;
		}
	}
	else
	{
		validCC = false;
	}
	if(validCC == false) {
		if (document.trafficForm.sLanguage.value == "S"){
			alert("Su tarjeta de crédito no es válido.");
		}else{
			alert("Invalid Credit Card Number.");
		}
	}
	return(validCC);
}

	


// Check that a string contains only numbers
function isNumeric(string, ignoreWhiteSpace) {
	if (string.search) {
		if ((ignoreWhiteSpace && string.search(/[^\d\s]/) != -1) || (!ignoreWhiteSpace && string.search(/\D/) != -1)) return false;
	}
	return true;
}




function displayNewWindow(thisObj) {
	window.open(thisObj.href,'offworld','width=630,height=500,directories=yes,resizable=yes,status=yes,menubar=yes,location=yes,scrollbars=yes'); 
	return false;
}


//Check for numeric values
function isNumericValue (s,FieldName)

{   var i;

    // Search through string's characters one by one
    // until we find a non-numeric character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);

        if (!isDigit(c)) {
		if (document.trafficForm.sLanguage.value == "S"){
			alert(FieldName + " debe ser numérico.");
		}else{
			alert(FieldName + " must be numeric.");
		}
		return false;
	}
    }

    // All characters are numbers.
    return true;
}

// Returns true if character c is a digit 
// (0 .. 9).

function isDigit (c)
{   return ((c >= "0") && (c <= "9"))
}



//Check for US zipcode
function isZipCode (s,FieldName)

{   
	if (s.length != 5) {
		if (document.trafficForm.sLanguage.value == "S"){
			alert(FieldName + " Debe ser un código postal de EE.UU. de 5 dígitos." );
		}else{
			alert(FieldName + " must be a 5 digit U.S. Zip Code." );
		}
		return false;
	}
    
    return true;
}

//Pop-up message for cancel button
function checkCancel(param) 
{

	var msg = "You are about to cancel this transaction.\r\n\r\nTo cancel this transaction, click \'OK\'.\r\nTo continue processing this transaction, click \'Cancel\'.";
	if (param == "S"){
		msg="Esta a punto de cancelar esta transacción.\r\n\r\nSi desea cancelarla, haga clic en \'OK\'.\r\nSi desea seguir procesando esta transacción, haga clic en \'Cancel\' (cancelar).";
	}
	if(confirm(msg)) {
		
		document.location.replace('searchCourt.asp'); 
		return true;
	} else {
		
		return false;
	}

}

