/* start trim */
	// Variable constant

  function trim(inputString) {
   // Removes leading and trailing spaces from the passed string. Also removes
   // consecutive spaces and replaces it with one space. If something besides
   // a string is passed in (null, custom object, etc.) then return the input.
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   return retValue; // Return the trimmed string back to the user
} // Ends the "trim" function

/* end trim*/


function trimSpace(inputString,object) {
   // Removes leading and trailing spaces from the passed string and set value in object. Also removes
   // consecutive spaces and replaces it with one space. If something besides
   // a string is passed in (null, custom object, etc.) then return the input.
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   object.value = retValue;
   return retValue; // Return the trimmed string back to the user
} // Ends the "trim" function

/*************** Start Check Box ***************/
 function ToggleAll(e,x){

	if (e.checked) {
	    CheckAll(e.name,x);
	}
	else {
	    ClearAll(e.name,x);
	}
  }

 function Check(e){
	e.checked = true;
 }

 function Clear(e){
	e.checked = false;
 }

 function CheckAll(nameCheckBox,nameCk){

	var ml = document.forms[0];
	var len = ml.elements.length;
	var e;
	if (len){
		for (var i = 0; i < len; i++) {
			e = ml.elements[i];
			if (e.name == nameCk && !(e.disabled)) {
				Check(e);
			}
		}
	}else {
		e = ml.checkbox;
		if (!(e.disabled)){
			Check(e);
		}
	}

	nameCheckBox.checked = true;
 }

 function ClearAll(nameCheckBox,nameCk){
	var ml = document.forms[0];
	var len = ml.elements.length;
	var e;
	if (len) {
		for (var i = 0; i < len; i++) {
			e = ml.elements[i];
			if (e.name == nameCk) {
				Clear(e);
			}
		}
	} else {
		Clear(ml.checkbox);
	}
		nameCheckBox.checked = false;
    }

function Toggle(e,x){
	if (e.checked) {
		x.checked = AllChecked(e.name);
	}
	else {
		x.checked = false;
	}
}

function AllChecked(x)
{
	ml = document.forms[0];
	len = ml.elements.length;
	if (len)
	{
		for(var i = 0 ; i < len ; i++) {
			if (ml.elements[i].name == x && !ml.elements[i].checked && !ml.elements[i].disabled) {
				return false;
			}
		}
		return true;
    }
	else {
		if (ml.checkbox.checked && !ml.checkbox.disabled)
		{
			return false;
		} else return true;
	}
}

/*************** End Check Box ***************/


/*************** Start Change Page ***************/

function  ChangePage() {
	var form = document.forms[0];
	form.mode.value = "SEARCH";
	form.MC_Page.value = form.MC_PageChange.value;
	alert(""+form.MC_Page.value );
	form.submit();
}

function NextPage() {
	var form = document.forms[0];
	form.mode.value = "SEARCH";
	form.MC_Page.value = parseInt(form.MC_PageChange.value) + 1;
	form.submit();
}

function PrevPage() {
	var form = document.forms[0];
	form.mode.value="SEARCH"
	form.MC_Page.value = parseInt(form.MC_PageChange.value) - 1;
	form.submit();
}

/*************** End Change Page ***************/


/*************** Start Check Delete ***************/

function checkSelect (){
	var blankCheck=false;
	var form = document.forms[0];

	if ( form.MC_CheckBox.length) {
		for ( var intCancel = 0; intCancel < form.MC_CheckBox.length; intCancel++ ) {
			if (form.MC_CheckBox[intCancel].checked == true)
			{
				blankCheck=true;
				break;
			}
		}
	}	else  {
		if ( form.MC_CheckBox.checked == true )
		{
			blankCheck = true;
		}
	}

	if (blankCheck == true)
{
	if (confirm(" Are you sure you want to delete record(s) ?")== true)
	{
		return true;
	}
	else
	{
	     return false;
	}
}
else
 {
	alert(" You didn't select any record . Please check box next to record(s) you want to select .");
    return false;
 }
}
/*************** End Check Delete ***************/


/* check length textarea */
function CheckLength(length) {
	var abc=window.event.srcElement.value
	var MAXLENGTH=length
	if (window.event.srcElement.value.length >= length) {
		window.event.srcElement.value=abc.substring(0,MAXLENGTH)
        return false;
	}
}

/** check Email address **/
  function checkEmail(checkThisEmail) {
	var myEMailIsValid = true;
	var myAtSymbolAt = checkThisEmail.indexOf('@');
	var myLastDotAt = checkThisEmail.lastIndexOf('.');
	var mySpaceAt = checkThisEmail.indexOf(' ');
	var myLength = checkThisEmail.length;


	// at least one @ must be present and not before position 2
	// @yellow.com : NOT valid
	// x@yellow.com : VALID

	if (myAtSymbolAt < 1 )
	 {myEMailIsValid = false}


	// at least one . (dot) afer the @ is required
	// x@yellow : NOT valid
	// x.y@yellow : NOT valid
	// x@yellow.org : VALID

	if (myLastDotAt < myAtSymbolAt)
	 {myEMailIsValid = false}

	// at least two characters [com, uk, fr, ...] must occur after the last . (dot)
	// x.y@yellow. : NOT valid
	// x.y@yellow.a : NOT valid
	// x.y@yellow.ca : VALID

	if (myLength - myLastDotAt <= 2)
	 {myEMailIsValid = false}


	// no empty space " " is permitted (one may trim the email)
	// x.y@yell ow.com : NOT valid

	if (mySpaceAt != -1)
	 {myEMailIsValid = false}


	if (myEMailIsValid == true){
		//alert("email is VALID")
	}
	else
	 {alert("Invalid email address!")}
	return myEMailIsValid
}


function checkUrl(str) {
	var position = -1;
	var urlFormat = "http://";
	position = str.indexOf(urlFormat);
	if (position != 0)
	{
		alert("Please fill URL begin with http:// ");
		return false;
	} else return true;
}

function checkMobile(str) {
	var position = -1;
	var urlFormat = "+";
	position = str.indexOf(urlFormat);
	var thelength = str.length;
	var number = str.substring(position+urlFormat.length,thelength);
	if (position != 0)
	{
		alert("Please fill \"+\" in first Position on phone ");
		return false;
	} else {
		if (isNaN(number))
		{
			alert("Please fill phone in Digit !");
		}	else	return true;
	}
}

function checkNumber(str) {
	if (isNaN(str))
	{
		alert("Please fill Number in Digit ");
		return false;
	} else return true;
}






/*************** Start Check Select Option ***************/
function checkSelectOption(strConfirm, strAlert){
	var blankCheck=false;
	var form = document.forms[0];

	if ( form.MC_CheckBox.length) {
		for ( var intCancel = 0; intCancel < form.MC_CheckBox.length; intCancel++ ) {
			if (form.MC_CheckBox[intCancel].checked == true) {
				blankCheck=true;
				break;
			}
		}
	}	else {
		if ( form.MC_CheckBox.checked == true ) {
			blankCheck = true;
		}
	}

	if (blankCheck == true) {
	  if (confirm(strConfirm)== true) {
		  return true;
	  } else {
	    return false;
	  }
  } else {
	  alert(strAlert);
    return false;
  }
}

//****************Check file browse***************//
function checkFileType(obj , list){
  var dotIndex = obj.value.lastIndexOf('.');
	var typeFile = obj.value.substring(dotIndex+1,obj.length) ;
	var resultCheck = false;
	var fileTypeAcceptList = list;
	var typeSize = fileTypeAcceptList.length;
	for(i=0;i<typeSize;i++){
		if(typeFile.toLowerCase()==fileTypeAcceptList[i]){
			resultCheck = true;
			break;
		}
	}
	return resultCheck;
}
//****************Check file browse***************//