function refreshParent() {
window.opener.location.href = window.opener.location.href;

if (window.opener.progressWindow)

{
window.opener.progressWindow.close()
}
window.close();
}
	
function getexpirydate(nodays){
var UTCstring;
Today = new Date();
nomilli=Date.parse(Today);
Today.setTime(nomilli+nodays*24*60*60*1000);
UTCstring = Today.toUTCString();
return UTCstring;
}

function setcookie(name,value,duration){

var duration_time = getexpirydate(duration);
//alert(duration_time);
cookiestring=name+"="+escape(value)+";EXPIRES="+duration_time
document.cookie=cookiestring;
}

function getcookie(cookiename) {
 var cookiestring=""+document.cookie;
 var index1=cookiestring.indexOf(cookiename);
 if (index1==-1 || cookiename=="") return ""; 
 var index2=cookiestring.indexOf(';',index1);
 if (index2==-1) index2=cookiestring.length; 
 return unescape(cookiestring.substring(index1+cookiename.length+1,index2));
}


function set_availability(what,which){
	
	var myElement = eval(what);
	
	if (which=="on") myElement.disabled=false;
	else if (which=="off") myElement.disabled=true;
	
}

function trimAll(sString) 
{
	while (sString.substring(0,1) == ' ')
	{
	sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == ' ')
	{
	sString = sString.substring(0,sString.length-1);
	}
	return sString;
}

function getIndex(what,which) {
    for (var i=0;i < what.elements.length;i++)
        if (what.elements[i].name == which)
            return i;
    return -1;
}

function getCheckedValue(what){

	var myElement = eval(what);
	
	for (i=0; i<myElement.length; i++){	
			//alert(i);	
		if (myElement[i].checked == true){
			//alert(myRadio[i].value);			
			myValue=myElement[i].value;								
		} 
	}	
	
	return myValue;

}

function findOption($objSelect, $strNeedle, $strSearch) 
    { 
       var $_found = new Boolean( false ); 
 
        // If $objSelect is a nothing [''], convert to a NULL 
        if ($objSelect == '') 
            $objSelect = null; 
 
        // If $objSelect is a string, convert to Object Reference 
        // Then see if Object Reference exists 
        else if ( ( typeof ( $objSelect ) ) == 'string' ) 
            $objSelect = document.getElementById($objSelect); 
            
        // If we have nothing to deal with, just bale... 
        if ( ( $objSelect ) && ( $strNeedle ) && 
             ( ( $strSearch == 'text' ) || ( $strSearch == 
'value' ) ) ) 
        { 
 
            for (var $i = 0; $i < $objSelect.options.length; $i++) 
            { 
                if ( $strSearch == 'value' ) 
                    $objHayStack = $objSelect[$i].value; 
 
                else if ( $strSearch == 'text' ) 
                    $objHayStack = $objSelect[$i].text; 
 
                if ( $objHayStack == $strNeedle ) 
                { 
                    $_found = parseInt ( $i ); // We found a match 
                    break; 
                } 
            } 
        } 
 
        // Send back what we have 
        return $_found; 
}; 


function getOptionIndex(var1,var2){	
	
  	var listBox = $(var1);
  	var optionValue;
  	  	  	
  	for (i=0; i<listBox.length; i++){			
		optionValue='';
		
		optionValue = listBox.options[i].value;
		//alert('index='+i+', option value='+optionValue+', input value='+var2);	
				
		if (optionValue==var2) {		
			var indexVal = i;					
		}		
	}	
	return indexVal;	  	 	
}


function chkNum(fName,var2,var3) { 
    
    var tNum = fName.value.charAt(fName.value.length-1); 
    var rNum = ''; 
    var maxlength = parseInt(var2);
    var c_index = parseInt(var3);

    if(tNum.match(/[^0-9]/i)) { 
        for(var j = 0; j < fName.value.length - 1; j++) { 
            rNum += fName.value.charAt(j); 
        }            
        //alert("Please enter only numbers"); 
        fName.value = rNum; 
        fName.focus();
        
    } else {   	
    	
		if(fName.value.length >= maxlength) {
			fName.form[c_index+1].focus();
			//fName.form[c_index+1].value='';
		}	
	}          
} 

function URLEncode(var1)
{

	// The Javascript escape and unescape functions do not correspond
	// with what browsers actually do...
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";

	var plaintext = trimAll(var1);
	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "%20";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
			    alert( "Unicode Character '" 
                        + ch 
                        + "' cannot be encoded using standard URL encoding.\n" +
				          "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (%20) will be substituted." );
				encoded += "%20";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for
	
	//alert(encoded);
	return encoded;
};

function URLDecode(var1)
{
   // Replace + with ' '
   // Replace %xx with equivalent character
   // Put [ERROR] in output if %xx is invalid.
   var HEXCHARS = "0123456789ABCDEFabcdef"; 
   var encoded = var1;
   var decoded = "";
   var i = 0;
   while (i < encoded.length) {
       var ch = encoded.charAt(i);
	   if (ch == "+") {
	       decoded += " ";
		   i++;
	   } else if (ch == "%") {
			if (i < (encoded.length-2) 
					&& HEXCHARS.indexOf(encoded.charAt(i+1)) != -1 
					&& HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
				decoded += unescape( encoded.substr(i,3) );
				i += 3;
			} else {
				alert( 'Bad escape combination near ...' + encoded.substr(i) );
				decoded += "%[ERROR]";
				i++;
			}
		} else {
		   decoded += ch;
		   i++;
		}
	} // while
   
   return decoded;
}


function insertOptionAt (select, option, index) {

    for (var i = select.options.length; i > index; i--) select.options[i] = select.options[i - 1];
    
    select.options[index] = option;
  
}

function deleteOptionAt (select){

  	for (var i = select.options.length; i > 0 ; i--) { // roop through the number of options
		  select.options[i] = null;
		}  
  }

function selectListBoxByValue(box_name,option_value){		

		var listBox = $(box_name);
		
		for (i=0; i<listBox.length; i++){
			
			if (listBox.options[i].value==option_value) {			
					
				listBox.options[i].selected=true;
									
			}				
		}	
	}	

function selectListBoxByIndex(box_name,option_index){
		
		//alert(index);
		//alert(var_name+','+var_index);	
		var listBox = $(box_name);
		myOption = '';
		for (i=0; i<listBox.length; i++){
			if (i==option_index) {
				myOption = i;
			}
			
		}		
		listBox.options[myOption].selected=true;	
	}	

	
function toggle(id_name){

	if (document.getElementById(id_name).style.display=='none'){
	
		document.getElementById(id_name).style.display='block';
		
	} else document.getElementById(id_name).style.display='none';
}

function select_chkbox(checkbox_name){

	var myCheckbox = $(checkbox_name);
	
	if (myCheckbox.checked == true) myCheckbox.checked = false;	
	else myCheckbox.checked = true;	

}

function select_radio(what,radio_value){

	var myRadio = eval(what);
	
	for (i=0; i<myRadio.length; i++){	
			//alert(i);	
		if (myRadio[i].value==radio_value){
			//alert(myRadio[i].value);	
			myRadio[i].checked = true;								
		} 
	}	
}


function selectRadioByValue(what,radioValue){

	var myRadio = eval(what);
	
	for (i=0; i<myRadio.length; i++){
	
		if (myRadio[i].value==radioValue) {
			myRadio[i].checked=true;
		}
	}	
}

function validateSelect(obj,container_id){
	
	for (var r=0; r < obj.length; r++){
		if ( obj.options[r].selected==false ) {
			
			obj.style.border='#A2B8D7 1px solid';
			if ($(container_id).style.visibility){			
				$(container_id).style.visibility='hidden';
			}
			else $(container_id).style.display='none';

			return true;
		}
		else {
			
			obj.style.border='red 1px solid';
			if ($(container_id).style.visibility){			
				$(container_id).style.visibility='visible';
			}
			else $(container_id).style.display='block';
			
			$(container_id).style.color='#ED1B23';
			$(container_id).style.backgroundColor='#FDF9CC';
			$(container_id).innerHTML = 'Please select at least one option';
			obj.focus();
			return false;
		
		}
	}				
}

function validateRadio(what,container_id){
	
	var obj = eval(what);
	var is_checked = false;
	
	for (var r=0; r < obj.length; r++){
		if ( obj[r].checked==true) is_checked = true;	
		//alert(obj[r].value);		
	}
	
	if ( is_checked==true ) {			
			
			if ($(container_id).style.visibility){			
				$(container_id).style.visibility='hidden';
			}
			else $(container_id).style.display='none';
			
			return true;
		}
		else {			
			
			if ($(container_id).style.visibility){			
				$(container_id).style.visibility='visible';
			}
			else $(container_id).style.display='block';
			
			$(container_id).style.color='#ED1B23';
			$(container_id).style.backgroundColor='#FDF9CC';
			$(container_id).innerHTML = 'Please check at least one option';
			
			return false;
		
		}
				
}

function validateCheckbox(what,container_id){
	
	var obj = eval(what);
		
	if ( obj.checked==true ) {			
			
			if ($(container_id).style.visibility){			
				$(container_id).style.visibility='hidden';
			}
			else $(container_id).style.display='none';
			
			return true;
	}
	else {			
		
		if ($(container_id).style.visibility){			
			$(container_id).style.visibility='visible';
		}
		else $(container_id).style.display='block';
		
		$(container_id).style.color='#ED1B23';
		$(container_id).style.backgroundColor='#FDF9CC';
		$(container_id).innerHTML = 'Please tick on this checkbox';
		
		return false;
	
	}
				
}


function validateTextBox(obj,type,min,max,container_id){
	
	//var result = new Array() ;
	
	var is_valid = true;
	var err_message;
	var old_message = $(container_id).innerHTML;
	
	//alert(old_message);

	if (obj.value){
	
		var str = obj.value;
		
		if (type == "string"){			
			
			for(var i = 0; i < str.length; i++) { 
	 
        		var str_char = str.charAt(i); 
		    	if(str_char.match(/[^a-zA-Z-_ ]/i)) { 
					is_valid = false;
					err_message = 'Please enter only text value into this field';
		    	}		    	
	  		} 			
		} else if (type == "number"){
			
			for(var i = 0; i < str.length; i++) { 
	 
        		var str_char = str.charAt(i); 
		    	if(str_char.match(/[^0-9]/i)) { 
					is_valid = false;
					err_message = 'Please enter only numbers into this field';
					
		    	}
	  		} 			
		} else if (type == "strnum"){
			
			for(var i = 0; i < str.length; i++) { 
	 
        		var str_char = str.charAt(i); 
		    	if(str_char.match(/[^0-9a-zA-Z-_ ]/i)) { 
					is_valid = false;
					err_message = 'Please enter only Alphabet characters and numbers in this field';
					
		    	}
	  		} 		
		}
		
		if (is_valid == true){
		
			if (min) {
			
				if (str.length < min) {
				
					is_valid = false;
					err_message = 'Please enter at least '+min+' characters in this field';
				
				}		
			}
			
			if (max) {
			
				if (str.length > max) {
				
					is_valid = false;
					err_message = 'Please enter less than '+max+' characters in this field';
				
				}		
			}
		}
		
		//alert(is_valid);
		
		if (is_valid==false){
		
			obj.style.border='red 1px solid';
			if ($(container_id).style.visibility){			
				$(container_id).style.visibility='visible';
			}
			else $(container_id).style.display='block';
			
			$(container_id).style.color='#ED1B23';
			$(container_id).style.backgroundColor='#FDF9CC';
			$(container_id).innerHTML = err_message;
			obj.focus();
			
			return false;
			
		} else { // true
			
			//alert($(container_id).innerHTML);
			
				obj.style.border='#A2B8D7 1px solid';
				if ($(container_id).style.visibility){			
					$(container_id).style.visibility='hidden';
				}
				else $(container_id).style.display='none';
							
			return true; 
			
		}		
		
	}	
	else{ //false
	
		//alert(obj.name);
		
			obj.style.border='red 1px solid';
			if ($(container_id).style.visibility){			
				$(container_id).style.visibility='visible';
			}
			else $(container_id).style.display='block';
			
			$(container_id).style.color='#ED1B23';
			$(container_id).style.backgroundColor='#FDF9CC';
			$(container_id).innerHTML = 'Please enter valid value in this field';
			//obj.focus();
			return false;
	}

}
