// This function is used at the time of login
function checkLogin(){
	var formName
	formName = document.form1
	if (!(validateString(formName.AdminUsername, "Please Enter User Email Address")))
		return false;

	res = checkvalidemail("form1","AdminUsername","Please Enter Valid Email Address")
	if(res==false)
	{
		return false;
	}
	if (!(validateString(formName.AdminPassword, "Please Enter Password")))
		return false;
	Lenpassword = formName.AdminPassword.value.length;
	if (Lenpassword < 6)
	{
		alert("Password must be 6 characters");
		formName.AdminPassword.focus();
		return false;
	}
}

// This function is used in the forgot password page
function checkForgotPassword(){
	var formName
	formName = document.form1
	if (!(validateString(formName.AdminUsermail, "Please Enter Your Email Address")))
		return false;
	res = checkvalidemail("form1","AdminUsermail","Please Enter Valid Email Address")
	if(res==false)
	{
		return false;
	}
}

// This function is used in the add admin page
function IsValidate() {
	var formName 
	var res
	formName = document.addcolor
	if (!(validateString(formName.txt_username, "You must enter User Email Address")))
		return false;

	res = checkvalidemail("addcolor","txt_username","Please Enter valid email address")
	if(res==false)
	{
		return false;
	}
	Lenusername = formName.txt_username.value.length;
	if (Lenusername > 255)
	{
		alert("User Name should not be more then 255 characters");
		formName.txt_username.focus();
		return false;
	}

	if (!(validateString(formName.txt_password, "You must enter Password")))
		return false;
	Lenpassword = formName.txt_password.value.length;
	if (Lenpassword > 50)
	{
		alert("Password should not be more then 50 characters");
		formName.txt_password.focus();
		return false;
	}

	if (Lenpassword < 6)
	{
		alert("Password should not be less then 6 characters");
		formName.txt_password.focus();
		return false;
	}

	var iChars1 = "!@#$%^&*()+=-[]\\\';,./{}|\":<>?_" ;
	for (var i = 0; i < formName.txt_password.value.length; i++) {
		if (iChars1.indexOf(eval("formName.txt_password.value.charAt("+i+")")) != -1) {
		  	alert("Password : Only a to z , A to Z, 0 to 9 and space is allowed.");
		  	formName.txt_password.focus();
		  	formName.txt_password.value = '';
		  	return false;
		}
	}

// code for Access Rights,if partial is selected and no even single checkbox is selected
	var flage = false;
	var checkPartials = false;
	if(formName.elements.length!=0 && document.getElementById("rd_right1").checked == true){
		for(i=0; i<formName.elements.length; i++)
		{
			if(formName.elements[i].id == 'chk_right' && formName.elements[i].checked == false)
			{
				flage = false;
			}else if(formName.elements[i].id == 'chk_right' && formName.elements[i].checked == true)
			{
				flage = true;
				checkPartials = true;
			}
		}
		if(checkPartials !=true)
		{
			alert("please select anyone checkbox");
			return false;
		}
	}
}

// This function is used in the Edit admin page
function IsValidateEdit()
{
// code for Access Rights,if partial is selected and no even single checkbox is selected
	var formName
	var res
	formName = document.addcolor
	var flage = false;
	var checkPartials = false;
	if(formName.elements.length!=0 && document.getElementById("rd_right1").checked == true){
		for(i=0; i<formName.elements.length; i++)
		{
			if(formName.elements[i].id == 'chk_right' && formName.elements[i].checked == false)
			{
				flage = false;
			}else if(formName.elements[i].id == 'chk_right' && formName.elements[i].checked == true)
			{
				flage = true;
				checkPartials = true;
			}
		}
		if(checkPartials !=true)
		{
			alert("please select anyone checkbox");
			return false;
		}
	}
}

// This function is used in the add and edit admin page
function showrights()
{
	var formName
	formName = document.addcolor
	var partial			= document.getElementById('partial');
	var chkfull 	= formName.rd_right[0].checked;
	var chkpartial 	= formName.rd_right[1].checked;

	if(chkfull == true)
	{
		partial.style.display='none';
	}
	else if(chkpartial == true)
	{
		partial.style.display='';
	}
}

// This function is used in the add Purchese Order page
function IsValidatePurchaseOrder() {
	var formName
	var res
	formName = document.addPurchaseOrder
	LenPorder = formName.txtPurchaseOrder.value.length;
	/*if (!(validateString(formName.txtPurchaseOrder, "You must enter Purchase Order")))
		return false;
	if (LenPorder > 50)
	{
		alert("Purchase Order Code should not be more then 50 characters");
		formName.txtPurchaseOrder.focus();
		return false;
	}

	
	for (var i = 0; i < formName.txtPurchaseOrder.value.length; i++) {
		if (iChars.indexOf(eval("formName.txtPurchaseOrder.value.charAt("+i+")")) != -1) 	{
		  	alert("Purchase Order : Only a to z , A to Z, 0 to 9 and space is allowed.");
		  	formName.txtPurchaseOrder.focus();
		  	formName.txtPurchaseOrder.value = '';
		  	return false;
		}
	}*/

	var iChars = "!@#$%^&*()+=-[]\\\';,./{}|\":<>?_" ;
	LenSupplier = formName.txtSupplier.value.length;
	if (!(validateString(formName.txtSupplier, "You must enter Supplier")))
		return false;
	if(LenSupplier > 255)
	{
		alert("Supplier should not be more then 255 characters");
		formName.txtSupplier.focus();
		return false;
	}

	var iChars1 = "0123456789!@#$%^&*()+=-[]\\\';,./{}|\":<>?_" ;
	for (var i = 0; i < formName.txtSupplier.value.length; i++) {
		if (iChars1.indexOf(eval("formName.txtSupplier.value.charAt("+i+")")) != -1) {
			alert("Supplier : Only a to z , A to Z and space is allowed.");
			formName.txtSupplier.focus();
		  	formName.txtSupplier.value = '';
		  	return false;
	  	}
	}

	LenSupplierReference = formName.txtSupplierReference.value.length;
	if (!(validateString(formName.txtSupplierReference, "You must enter Supplier Reference")))
		return false;
    if(LenSupplierReference > 255)
    {
    	alert("Supplier Reference should not be more then 255 characters");
		formName.txtSupplierReference.focus();
		return false;
    }

	// var iChars1 = "!@#$%^&*()+=-[]\\\';,./{}|\":<>?_0123456789" ;
	for (var i = 0; i < formName.txtSupplierReference.value.length; i++) {
	  	if (iChars1.indexOf(eval("formName.txtSupplierReference.value.charAt("+i+")")) != -1) {
			alert("Supplier Reference : Only a to z , A to Z and space is allowed.");
			formName.txtSupplierReference.focus();
			formName.txtSupplierReference.value = '';
			return false;
	  	}
	}

    LenProduct = formName.txtProduct.value.length;
	if (!(validateString(formName.txtProduct, "You must enter Product")))
		return false;
	if(LenProduct > 255)
	{
		alert("Product should not be more then 255 characters");
		formName.txtProduct.focus();
		return false;
	}

	for (var i = 0; i < formName.txtProduct.value.length; i++) {
		if (iChars1.indexOf(eval("formName.txtProduct.value.charAt("+i+")")) != -1) {
			alert("Product : Only a to z , A to Z and space is allowed.");
			formName.txtProduct.focus();
			formName.txtProduct.value = '';
			return false;
		}
	}

	if(!(isNumber(formName.WeighttobeShipped,"Weight to be Shipped : Only Number is allowed.")))
	{
		return false;
	}

	LenPortOfLoading = formName.txtPortOfLoading.value.length;
	if(LenPortOfLoading > 255)
	{
		alert("Port Of Loading should not be more then 255 characters");
		formName.txtPortOfLoading.focus();
		return false;
	}

	for (var i = 0; i < formName.txtPortOfLoading.value.length; i++) {
		if (iChars1.indexOf(eval("formName.txtPortOfLoading.value.charAt("+i+")")) != -1) {
			alert("Port Of Loading : Only a to z , A to Z and space is allowed.");
			formName.txtPortOfLoading.focus();
			formName.txtPortOfLoading.value = '';
			return false;
		}
	}

	LenPortOfDelivery = formName.txtPortOfDelivery.value.length;
	if(LenPortOfDelivery > 255)
	{
		alert("Port Of Delivery should not be more then 255 characters");
		formName.txtPortOfDelivery.focus();
		return false;
	}

	for (var i = 0; i < formName.txtPortOfDelivery.value.length; i++) {
		if (iChars1.indexOf(eval("formName.txtPortOfDelivery.value.charAt("+i+")")) != -1) {
			alert("Port Of Delivery : Only a to z , A to Z and space is allowed.");
			formName.txtPortOfDelivery.focus();
			formName.txtPortOfDelivery.value = '';
			return false;
		}
	}
}

function IsValidatePurchaseOrder1()
{
	var formName
	var res
	formName = document.addPurchaseOrdercomment

	if (!(validateString(formName.purchaseOrd, "You must select Purchase Order")))
		return false;

	if (!(validateString(formName.txtcomments, "You must enter Purchase Order comments")))
		return false;
}

// This function user for check the searchbox is not empty
function MgtValidation()
{
	var formName
	formName = document.frmpom
	if(!(validateString(formName.POsearch, "Please Enter Purchase Order for Search")))
		return false;
}

// This Function is used for the change password
function Check_ChgPasswd()
{
	var old_passwd;
	var new_passwd;
	var conf_passwd;

	old_passwd = CheckingNull("frmchgpass","old_passwd","Enter Valid Old Password");
	if(old_passwd == false)
	{
		return false;
	}

	new_passwd = CheckingNull("frmchgpass","new_passwd","Enter Valid New Password");
	if(new_passwd == false)
	{
		return false;
	}

	conf_passwd = CheckingNull("frmchgpass","conf_passwd","Enter Valid Confirm Password");
	if(conf_passwd == false)
	{
		return false;
	}
	else
	{
		if (document.frmchgpass.new_passwd.value != document.frmchgpass.conf_passwd.value)
		{
			alert("New Password & Confirm Password should be same.");
			document.frmchgpass.conf_passwd.value = '';
			document.frmchgpass.conf_passwd.focus();
			return false;
		}
	}
}

function Check_User_Login()
{
	var ueresult;
	var ueresultnull;
	ueresultnull = CheckingNull("frm_login","txt_log_email","Enter Valid Email Address");

	if(ueresultnull == false)
	{
		return false;
	}
	else
	{
		ueresult = checkvalidemail("frm_login","txt_log_email","Enter Valid Email Address");
		if(ueresult == false)
		{
			return false;
		}
	}

	var chkpasswordnull;
	chkpasswordnull = CheckingNull("frm_login","txt_log_password","Enter Password");

	if(chkpasswordnull == false)
	{
		return false;
	}
}

function uncheckedforOthers(frm,index)
{
	with(frm)
	{
		var allcat = document.getElementById("Cat["+index+"]");
		if(allcat.checked)
		{
			allcat.checked = false;
		}
 	}
}

function uncheckedAll(){
	if(document.frmscooterselect.allbrandname.checked==true)
	{
		document.frmscooterselect.allbrandname.checked = false;
	}
}

function uncheckedOthers(){
	var comp="";
// count the no of elements in a form
	var num_elements = document.frmscooterselect.elements.length;

	if(document.frmscooterselect.allbrandname.checked == true) {
		for(i=0;i<num_elements;i++) {
			if (document.frmscooterselect.elements[i].type == 'checkbox' && document.frmscooterselect.elements[i].name != "allbrandname" )
			{
				if(document.frmscooterselect.elements[i].checked==true)
				{
					document.frmscooterselect.elements[i].checked=false;
				}
			}
		}
	}
}

function notNull(str){
	if (str.length == 0 )
		return false
	else{
		return true
	}
}

function notBlank(str){
	for (i = 0; i < str.length; i++){
		if (str.charAt(i) != " ")
			return true
	}
	return false
}

function validateString(myfield, message){
	if (notNull(myfield.value)&& notBlank(myfield.value))
		return true
	else{
		alert(message )
		myfield.focus()
		return false
	}
}

function isNumber(myfield, message){
	str=myfield.value

	numdecs = 0;

	for (i = 0; i < str.length; i++){
		mychar = str.charAt(i)

		if ((mychar >= "0" && mychar <= "9")){
			numdecs++
		}
		else if (mychar == "."){
			numdecs++
		}
		else{
			alert(message )
			myfield.value=''
			myfield.focus()	
			return false;
		}
	}
	return true
}

function checkvalidemail(formname,fieldname,message)
{
	var emails=/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
	var membermailid = eval("document."+formname+"."+fieldname+".value");
	result1=membermailid.search(emails);
	if(result1==-1)
	{
		alert(message);
		eval("document."+formname+"."+fieldname+".focus()");
		eval("document."+formname+"."+fieldname+".value"+"=''");
		return false;
	}
	return true;
}

function CheckingNull(formname,fieldname,message)
{
	var fieldval = eval("document."+formname+"."+fieldname+".value");

	fieldval= fieldval.replace(/^\s+|\s+$/g, "");

	if( fieldval == '')
	{
	  	alert(message);
	  	eval("document."+formname+"."+fieldname+".value"+"=''");
	  	eval("document."+formname+"."+fieldname+".focus()");
	 	return false;
	 }
	 return true;
}

function Trim(Value)
{
	return Value.replace(/^\s+|\s+$/g, "");
}

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;
} //End Function

function openpopup(url,popup_name,height,width,other_properties)
{
	var left		= parseInt((screen.width-350)/2);
	var top			= parseInt((screen.height-300)/2)
	var win_options = 'height='+height+',width='+width+',resizable=no,'
	+ 'scrollbars=no,left=' + left + ',top=' + top;
	window.open(url,popup_name,win_options);
}