/*------------------
Email validation
--------------------*/


function isEmailAddr(email) {
  var result = false;
  var theStr = new String(email);
  var index = theStr.indexOf("@");
  if (index > 0) {
    var pindex = theStr.indexOf(".",index);
    if ((pindex > index+1) && (theStr.length > pindex+1))
	result = true;
  }
  return result;
}


function validEmail(formField,fieldLabel,required) {
	var result = true;

	if (required && !validRequired(formField,fieldLabel))
		result = false;

	if (required)	{
		if (result && ((formField.value.length < 3) || !isEmailAddr(formField.value)) )	{
			alert("Please enter a complete email address in the form: yourname@yourdomain.com");
			formField.focus();
			result = false;
		}
	}
   
  return result;
}

/*------------------
text box validation
--------------------*/

function validRequired(formField,fieldLabel){
	var result = true;
	var sf=formField.value;
	var sff=sf.replace(/^\s+/g, '');
	if (sff == "") {
		alert('Please enter  the "' + fieldLabel +'" to proceed.');
		formField.focus();
		result = false;
	}	
	return result;
}

/*------------------
selection box validation
--------------------*/

function validSelection(formField,fieldLabel){
	var result = true;

	if (formField.options[0].selected) {
		alert('Please select a value for "' + fieldLabel +'" to proceed.');
		formField.focus();
		result = false;
	}	
	return result;
}

/*------------------
Hide error msg table
--------------------*/

function hidelaptop(theTable)
{
     if (document.getElementById(theTable).style.display == 'none')
     {
          document.getElementById(theTable).style.display = 'none';
     }
     else
     {
          document.getElementById(theTable).style.display = 'none';
     }
}

/*------------------
Open popup window
--------------------*/

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

/*------------------
Display alert message
--------------------*/

function MM_popupMsg(msg) { //v1.0
  alert(msg);
}

/*------------------
for confirmation box
--------------------
*/
function dialogBox(msg) {
	var is_confirmed = confirm(msg);
	if (is_confirmed) { return true;
	} else {return false;}
}

/*------------------
Radio button validation
--------------------
*/

function validRadioSelected(formField,fieldLabel) {
	var radio_choice = false;	
	if(typeof(formField.length)=="undefined"){
		if (formField.checked)
			radio_choice = true; 
	}
	for (counter = 0; counter < formField.length; counter++) {
		if (formField[counter].checked)
			radio_choice = true; 
	}

	if (!radio_choice) {
		alert('Please select an option for "' + fieldLabel + '" field.');
	} 
	return radio_choice;
}

/*---------------------
Is a Number
-----------------------
*/

function validNum(formField,fieldLabel,required) {
	var result = true;
	if (required && !validRequired(formField,fieldLabel))
		result = false;
	if (result)	{
 		if (!allDigits(formField.value)) {
 			alert('Please enter a number for the "' + fieldLabel +'" field.');
			formField.focus();		
			result = false;
		}
	} 
	return result;
}
/*---------------------
Is a Number
-----------------------
*/

function validPhone(formField,fieldLabel,required) {
	var result = true;
	if (required && !validRequired(formField,fieldLabel))
		result = false;
	if (result)	{
 		if (!allPhone(formField.value)) {
 			alert('Please enter a valid value for the "' + fieldLabel +'" field.');
			formField.focus();		
			result = false;
		}
	} 
	return result;
}


/*------------------------
all digits
-------------------------
*/
function allDigits(str) {
	return inValidCharSet(str,"0123456789");
}

/*---------------------
Character SEt
------------------------
*/
function inValidCharSet(str,charset){
	var result = true;

	// Note: doesn't use regular expressions to avoid early Mac browser bugs	
	for (var i=0;i<str.length;i++)
		if (charset.indexOf(str.substr(i,1))<0)	{
			result = false;
			break;
		}
	
	return result;
}


/*---------------------
Check box validation
------------------------
*/

function validCheckbox(formField,fieldLabel) {
	var result = true;
	
	if (! formField.checked) {
		alert('Please select the "' + fieldLabel +'" field.');
		formField.focus();
		result = false;
	}	
	return result;
}


/*---------------------
Float validation
------------------------
*/
function validFloat(formField,fieldLabel, required) {
	var result = true;
	
	if (required && !validRequired(formField,fieldLabel))
		result = false;

	if (result)	{
 		if (!allFloat(formField.value)) {
 			alert('Please enter a number for the "' + fieldLabel +'" field.');
			formField.focus();		
			result = false;
		}
	} 
	return result;
} 


/*--------------------
All float values
-----------------------
*/


function allFloat(str) {
	return inValidCharSet(str,"0123456789.");
}


/*---------------------
Name validation
------------------------
*/
function validName(formField,fieldLabel) {
	var result = true;
	
	if (!validRequired(formField,fieldLabel))
		result = false;

	if (result)	{
 		if (!allName(formField.value)) {
 			alert("Invalid character(s) entered, only alphanumeric, space, and -.,'@()& are allowed");
			formField.focus();		
			result = false;
		}
	} 
	return result;
} 


/*--------------------
All Name values
-----------------------
*/


function allName(str) {
	return inValidCharSet(str,"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-,.'@()& ");
}



/*---------------------
Address validation
------------------------
*/
function validAddress(formField,fieldLabel) {
	var result = true;
	
	if (!validRequired(formField,fieldLabel))
		result = false;

	if (result)	{
 		if (!allAddress(formField.value)) {
 			alert("Invalid character(s) entered, only alphanumeric, space, and -,./#@+&' are allowed");
			formField.focus();		
			result = false;
		}
	} 
	return result;
} 


/*--------------------
All Address values
-----------------------
*/


function allAddress(str) {
	return inValidCharSet(str,"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-,./#+'@& ");
}


/*---------------------
City validation
------------------------
*/
function validCity(formField,fieldLabel) {
	var result = true;
	
	if (!validRequired(formField,fieldLabel))
		result = false;

	if (result)	{
 		if (!allCity(formField.value)) {
 			alert("Invalid characters. Only alphanumeric, spaces and -.,' are allowed");
			formField.focus();		
			result = false;
		}
	} 
	return result;
} 


/*--------------------
All City values
-----------------------
*/


function allCity(str) {
	return inValidCharSet(str,"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-,.' ");
}

/*--------------------
All phone values
-----------------------
*/


function allPhone(str) {
	return inValidCharSet(str,"0123456789-");
}



/*---------------------
Doamin validation
------------------------
*/
function validDomain(formField,fieldLabel, required) {
	var result = true;
	

	if (result)	{
 		if (!allDomain(formField)) {
			result = false;
		}
	} 
	return result;
} 


/*--------------------
All Domain values
-----------------------
*/


function allDomain(str) {
	return inValidCharSet(str,"0123456789-.abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
}



/*--------------------
All Alpha values
-----------------------
*/


function allAlpha(str) {
	return inValidCharSet(str,"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
}




/*--------------------
Function to truncate values based on screen width.
-----------------------
*/

function setPx(per)
{
	x=screen.width;
	y = (x/100)*per-23;
	document.write("<div style='width:" + Math.round(y).toString() + "px;overflow:hidden;'>");
}

function showCharc1(strval) { 
	var sc_width = screen.width; 
	
	if (sc_width == 800)
		len = 38;
	else if (sc_width == 1024)
		len = 40;
	else if (sc_width == 1152)
		len = 45;
	else if (sc_width == 1280)
		len = 50;
		
	if (strval.length > len) {
		str1 = strval.substr(0, len-3); 
		strval = str1 + "...";
	}
	document.write(strval);
}

function showCharc2(strval) { 
	var sc_width = screen.width; 
	
	if (sc_width == 800)
		len = 24;
	else if (sc_width == 1024)
		len = 29;
	else if (sc_width == 1152)
		len = 32;
	else if (sc_width == 1280)
		len = 35;
		
	if (strval.length > len) {
		str1 = strval.substr(0, len-3); 
		strval = str1 + "...";
	}
	document.write(strval);
}

function showCharc3(strval) { 
	var sc_width = screen.width; 
	
	var len;
	if (sc_width == 800)
		len = 22;
	else if (sc_width == 1024)
		len = 25;
	else if (sc_width == 1152)
		len = 30;
	else if (sc_width == 1280)
		len = 35;

		
	if (strval.length > len) {
		str1 = strval.substr(0, len-3); 
		strval = str1 + "...";
	}
	document.write(strval);
}

function showCharc4(strval) { 
	var sc_width = screen.width; 
	
	if (sc_width == 800)
		len = 30;
	else if (sc_width == 1024)
		len = 35;
	else if (sc_width == 1152)
		len = 40;
	else if (sc_width == 1280)
		len = 40;
		
	if (strval.length > len) {
		str1 = strval.substr(0, len-3); 
		strval = str1 + "...";
	}
	document.write(strval);
}

function showCharc5(strval) { 
	var sc_width = screen.width; 
	
	if (sc_width == 800)
		len = 43;
	else if (sc_width == 1024)
		len = 46;
	else if (sc_width == 1152)
		len = 51;
	else if (sc_width == 1280)
		len = 56;
		
	if (strval.length > len) {
		str1 = strval.substr(0, len-3); 
		strval = str1 + "...";
	}
	document.write(strval);
}


function showCharc6(strval) { 
	var sc_width = screen.width; 
	
	if (sc_width == 800)
		len = 16;
	else if (sc_width == 1024)
		len = 18;
	else if (sc_width == 1152)
		len = 20;
	else if (sc_width == 1280)
		len = 25;
		
	if (strval.length > len) {
		str1 = strval.substr(0, len-3); 
		strval = str1 + "...";
	}
	document.write(strval);
}
