// JavaScript Document

var field_main;

function do_blink(field)
{
	var errorcolor="#ff0000";
	field.focus();
	field.select();
	field.style.background=errorcolor;
    field_main=field;
	setInterval("temp()",500);    
}

function temp(field)
{
	var okcolor="#ffffff";
	field_main.style.background=okcolor;
}

function validate(field)
{
	
	try
	{
	valiclass=field.getAttribute("valiclass");
	valimessage=field.getAttribute("valimessage");
	if(valiclass=="required")
	{
		req=field.getAttribute("req");
		pattern="\\w{"+req+",}";
		if(!field.value.match(pattern))
		{
			alert(valimessage);do_blink(field);return false;
		}
	}
	
	else if(valiclass=="email")
	{
		
		pattern="^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})";
		<!--pattern="^\\w{1,}@\\w{1,}(\\.\\w{1,}){1,}$";-->
		if(!field.value.match(pattern))
		{
			alert(valimessage);do_blink(field);return false;
		}
	}
	
	else if(valiclass=="number")
	{
		if((field.value.length<1)||isNaN(field.value))
		{
			alert(valimessage);do_blink(field);return false;
		}
	}
	
	else if(valiclass=="quantity")
	{
		if((field.value.length<1)|| (field.value.length>99) || isNaN(field.value) || field.value ==0)
		{
					alert(valimessage);do_blink(field);return false;
		}
	}
	
	else if(valiclass=="creditcard")
	{
		pattern="[0-9]{16}$";
		if(!field.value.match(pattern))
		{
			alert(valimessage);do_blink(field);return false;
		}
	}
	
	else if(valiclass=="zip")
	{
		pattern="[0-9]{5}$";
		if(!field.value.match(pattern))
		{
			alert(valimessage);do_blink(field);return false;
		}
	}
	
	else if(valiclass=="code")
	{
		pattern="[0-9]{3}$";
		if(!field.value.match(pattern))
		{
			alert(valimessage);do_blink(field);return false;
		}
	
	}
	
	else if(valiclass=="select")
	{
		if(field.options[field.selectedIndex].text.toLowerCase()=="select")
		{
			alert(valimessage);
			field.focus();
			return false;
		}		
	}
	
	
	else if(valiclass=="date")
	{
		pattern="[0-9]{4}\\-[0-9]{1,2}\\-[0-9]{1,2}$";
		if(!field.value.match(pattern))
		{
			alert(valimessage);do_blink(field);return false;
		}
	}

	else if(valiclass=="integer")
	{
		$bool=field.value.match("^[0-9]{1,4}$");
		
		if((!$bool)||isNaN(field.value)||(field.value.indexOf(".")!=-1))
		{
			alert(valimessage);do_blink(field);return false;
		}
	}

	else if(valiclass=="picture")
	{
		if(field.value.length<1)
			return true;
		values=field.value.toLowerCase();
		if(!ends_with(values,".jpg") && !ends_with(values,".jpeg") && !ends_with(values,".gif"))
		{
			alert(values+" is not a valid picture file");do_blink(field);return false;
		}
	}
	

else if (valiclass=="shipping") 
{
	if (!document.getElementById("shipping_0").checked && !document.getElementById("shipping_1").checked && !document.getElementById("shipping_2").checked && !document.getElementById("shipping_5").checked)
	{
		alert("Please Select Delivery Option");
		document.getElementById("shipping_0").focus();
		return false;
	}
}


return true;
	}
	
	catch(ex)
	{
	alert(ex.message);
	return true;
	}
}


function call_validate(form,from,to)
{
	//alert('sfgsdgdf');
	//alert(document.getElementById("qty").value);
	
	//if(document.getElementById("qty").value == "" || document.getElementById("qty").value == 0){
		//alert("Quantity cannot be empty");
		
		
	//}
	//else{

	loader = document.getElementById("disp");
	
	loader.style.display="block";

	for(counter=from;counter<to;counter++)
	{
			bool=validate(form[counter]);
			if(!bool)
			{
				loader.style.display="none";
				return false;
				break;
			}
	}
	

	form.submit();
	//}
}

/////////////////////
function getIndex(what,form) {
    for (var i=0;i<form.length;i++)
        if (what == form[i])
            return i;
    return -1;
}
////////////////////////////////////

function ends_with(hay,neddle)
{
	hay=hay.replace(/^\s*|\s*$/g,"");
	neddle_length=neddle.length;
	hay_length=hay.length;
	hay_part=hay.substring((hay_length-neddle_length),hay_length);
	return (hay_part==neddle)
}


function  enable_form(form,bools)
{
	for(counter=0;counter<form.length;counter++)
	{
		form[counter].disabled=!bools;	
	}
}


function loading(bool)
{
	try
	{
		comp=document.getElementById("loading");
		if(bool)
		{
			comp.style.visibility="visible";
		}
		else
		{
			comp.style.visibility="hidden";
		}
	}
	catch(d)
	{
		alert(d.message);	
	}
}

function validate_range()
{
	//alert(validate_range.arguments.length);	
	for(counter=0;counter<validate_range.arguments.length;counter++)
	{
		bool=validate(validate_range.arguments[counter]);
		if(!bool)
		{
			return false;
			break;
		}
	}
	validate_range.arguments[0].form.submit();
	return true;
}



function change_status(id,status,query)
{
	if(confirm("Are you sure you want to " + status + "?"))
	{
		location.replace("language_change_status.php?id="+id+"&"+"status="+status+"&"+query);	
	}
	return false;
}



function delete_sub_category(id,query)
{
	if(confirm("Are you sure you want to delete"))
	{
		location.replace("delete_sub_category.php?id="+id+"&"+query);	
	}
	return false;
}

function delete_main_category(id,query)
{
	if(confirm("Are you sure you want to delete"))
	{
		location.replace("delete_main_category.php?id="+id+"&"+query);	
	}
	return false;
}

function delete_article(id,query)
{
	if(confirm("Are you sure you want to delete"))
	{
		location.replace("delete_article.php?id="+id+"&"+query);	
	}
	return false;
}

function delete_product(id,query)
{
	if(confirm("Are you sure you want to delete"))
	{
		location.replace("delete_product.php?id="+id+"&"+query);	
	}
	return false;	
}

function delete_image(id,query,isAdd)
{
	if(confirm("Are you sure you want to delete"))
	{
		if (isAdd == "yes")
		{
			location.replace("delete_image.php?aid="+id+"&"+query);	
		}
		else
		{
			location.replace("delete_image.php?id="+id+"&"+query);	
		}
	}
	return false;	
}

function delete_homeimage(name,query)
{
	if(confirm("Are you sure you want to delete"))
	{
		location.replace("delete_homeimage.php?name="+name+"&"+query);	
	}
	return false;	
}

function manage_payment(div)
{
	divdebit_card.style.visibility="hidden";
	divcheque.style.visibility="hidden";
	divmoney_order.style.visibility="hidden";
	div.style.visibility="visible";
}


function delete_confirm_sales(delte_where,delete_what,id,return_where,date1,date2)
{
	alert("Sales once deleted are not recoverable");
	bool=confirm("Are you sure you want to delete " + delete_what+" ?");
	if(bool)
	{
		location.replace("delete_sales.php?delete_where="+delte_where+"&id="+id+"&return_where="+return_where+"&datefrom="+date1+"&dateto="+date2);
	}
	else
	{
		return false;	
	}
}