// JavaScript Document

var field_main;
function do_blink(field)
{
	var errorcolor="#1185B8";
	field.focus();
	field.select();
	field.style.background=errorcolor;
    field_main=field;
	setInterval("temp()",500);
  
    
}

function temp(field)
{
var okcolor="";
field_main.style.background=okcolor;
}

function validate(field)
{
	try
	{
	valiclass=field.getAttribute("valiclass");
	valimessage=field.getAttribute("valimessage");
	if(valiclass=="required")
	{
	
	req=field.getAttribute("req");
	//alert('asd'+valiclass);
	pattern="\\w{"+req+",}";
	if(!field.value.match(pattern))
	{
	alert(valimessage);do_blink(field);return false;
	}
	
	 }
	 else if(valiclass=="email")
 	{
 //pattern="^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$";
 if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(field.value)))
 {
 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=="telephone")
	{
		patternTel = "^[0-9()\\s-]+$";
		if(!field.value.match(patternTel)){
				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}\\-[1-9]{1,2}\\-[1-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==0)
	{
		alert("Please enter a non-zero number");
		do_blink(field);
		return false;
	}
	if((!$bool)||isNaN(field.value)||(field.value.indexOf(".")!=-1))
	{
	alert(valimessage);
	do_blink(field);
	return false;	
	}
}

else if(valiclass=="qty")
{
	$bool=field.value.match("^[1-9]{1,4}$");
	
if((!$bool)||isNaN(field.value)||(field.value<12))
{
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"))
			  {
alert(values+" is not a valid jpg file");do_blink(field);return false;
}

}
return true;
}
catch(ex)
{
alert(ex.message);
return true;
}
}



function call_validate(form,from,to)
{
for(counter=from;counter<to;counter++)
{
bool=validate(form[counter]);
if(!bool)
{
return false;
break;
}
}
form.submit();
}





function call_validate_ajax(form,from,to)
{
for(counter=from;counter<to;counter++)
{
bool=validate(form[counter]);
if(!bool)
{
return false;
break;
}
}
return true;
}


/////////////////////
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 delete_category(id,query)
{
if(confirm("Are you sure you want to delete"))
{
location.replace("delete_category.php?id="+id+"&"+query);	
}
return false;
}



function delete_mid_category(id,query)
{
if(confirm("Are you sure you want to delete"))
{
location.replace("delete_mid_category.php?id="+id+"&"+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_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)
{
if(confirm("Are you sure you want to delete"))
{
location.replace("delete_image.php?id="+id+"&"+query);	
}
return false;	
}


	
function delete_description(id,query)
{
if(confirm("Are you sure you want to delete"))
{
location.replace("delete_description.php?desp_id="+id+"&"+query);
}
return false;	
}

function delete_colors(id,query)
{
if(confirm("Are you sure you want to delete"))
{
location.replace("delete_colors.php?color_id="+id+"&"+query);
}	
}

function delete_color_image(id,query)
{
if(confirm("Are you sure you want to delete"))
{
location.replace("delete_color_image.php?pic_id="+id+"&"+query);
}	
}

function delete_size(id,query)
{
if(confirm("Are you sure you want to delete"))
{
location.replace("delete_size.php?size_id="+id+"&"+query);
}	
}


function manage_payment(div)
{
	divdebit_card = document.getElementById("divdebit_card");
	divdcheque = document.getElementById("divcheque");
	divmoney_order = document.getElementById("divmoney_order");
	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);
//alert("delete.php?delete_where="+delte_where+"&id="+id+"&return_where="+return_where+"&"+extra);
}
else
{
return false;	
}
}
function delete_client(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.php?delete_where="+delte_where+"&id="+id+"&return_where="+return_where+"&datefrom="+date1+"&dateto="+date2);
//alert("delete.php?delete_where="+delte_where+"&id="+id+"&return_where="+return_where+"&"+extra);
}
else
{
return false;	
}
}
function delete_confirm(id)
{
bool=confirm("do you want to delete the client");
if(bool)
{
href="delete.php?id="+id;
location.replace(href);
}
else
{
return;
}
}
function in_stock(id,query)
{
location.replace("in_stock.php?id="+id+"&"+query);	
return false;	
}

function out_of_stock(id,query)
{
location.replace("out_of_stock.php?id="+id+"&"+query);	
return false;	
}