// 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="^\\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<1)|| (field.value>99) || isNaN(field.value))
		{
			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=="select1")
	{
		if(field.value.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)||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;
		}
	}
	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 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_company(id,query)
{
	if(confirm("Are you sure you want to delete"))
	{
		location.replace("delete_company.php?id="+id+"&"+query);	
	}
	return false;
}

function delete_extras(id,query)
{
	if(confirm("Are you sure you want to delete"))
	{
		location.replace("delete_extras.php?id="+id+"&"+query);	
	}
	return false;
}

function delete_type(id,query)
{
	if(confirm("Are you sure you want to delete"))
	{
		location.replace("delete_type.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_catpics(name,query)
{
	if(confirm("Are you sure you want to delete"))
	{
		location.replace("delete_catpics.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;	
	}
}