// JavaScript Document// JavaScript Document

var field_main;

function do_blink(field)
{
	var errorcolor="#000000";
	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");
	//alert(valiclass);
	//alert(document.getElementById('writer_email').value);
	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")
	{
		if(document.getElementById('writer_email').value=="")
		{
			alert("Please Enter a Valid Email Address");
		return false;
		}
		else if(echeckName(document.getElementById('writer_email').value)=="false"){
		return false;
		}
	}
	
	else if(valiclass=="aaa")
	{
		if ("password".value!=="confirm_password".value)
	{
	alert(valimessage);do_blink(field);return false;
	}
		
		
		
		if(!field.value.match(pattern))
		{
			alert(valimessage);do_blink(field);return false;
		}
	}
	
	else if(valiclass=="url")
	{
		pattern = "http:\/\/www.[A-Za-z0-9\.-]{3,}\.[A-Za-z]{3}";
		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=="password")
	{
		if((field.value==null) || (field.value=="")||(field.value.length<5))
		{
					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=="Docfile")
	{
		if((field.value.lastIndexOf(".doc")==-1) && (field.value.lastIndexOf(".docx")==-1))
		{
					alert(valimessage);do_blink(field);return false;
		}
	}
	
	else if(valiclass=="imageFile")
	{
		if((field.value.lastIndexOf(".jpg")==-1) && (field.value.lastIndexOf(".gif")==-1) && (!field.value==""))
		{
					alert(valimessage);do_blink(field);return false;
		}
	}
	
	else if(valiclass=="zip")
	{
		if((field.value.length>5)|| (field.value.length>99) || isNaN(field.value) || field.value ==0)
		{
					alert(valimessage);do_blink(field);return false;
		}
	}
	
	else if(valiclass=="vid")
	{
		if((field.value.length>10)|| (field.value.length>99) || isNaN(field.value) || field.value ==0)
		{
					alert(valimessage);do_blink(field);return false;
		}
	}
	
	else if(valiclass=="phone")
	{
		pattern="[0-9]{1,25}$";
		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)||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 + "?"))
	{
		document.location = "language_change_status.php?id="+id+"&"+"status="+status+"&"+query;	
	}
	return false;
}

function delete_client(id)
{
	if(confirm("Are you sure you want to delete"))
	{
		document.location = "index2.php?action=new_client_request&del_id="+id;	
	}
	return false;
}

function checkPickup() {
if(confirm("Are you sure you want to pickup"))
{
document.location = "writer_index.php?action=../writer/available_ordert&id="+id;
}
return false;
} 


function delete_writer(id)
{
	if(confirm("Are you sure you want to delete"))
	{
		document.location = "index2.php?action=new_writer_request&del_id="+id;	
	}
	return false;
}

function delete_delete_testi(test_id)
{
	if(confirm("Are you sure you want to delete"))
	{
		document.location = "index2.php?action=new_testimonial_request&del_id="+test_id;	
	}
	return false;
}

function delete_sub_category(id)
{
	if(confirm("Are you sure you want to delete"))
	{
		document.location = "index2.php?action=delete_sub_category&id="+id;	
	}
	return false;
}

function delete_main_category(id,query)
{
	if(confirm("Are you sure you want to delete"))
	{		
		document.location = "index2.php?action=delete_main_category&id="+id;
	}
	return false;
}

function delete_product(id)
{
	if(confirm("Are you sure you want to delete"))
	{
		document.location = "index2.php?action=delete_product&id="+id;	
	}
	return false;	
}

function delete_manufacturer(id)
{
	if(confirm("Are you sure you want to delete"))
	{
		document.location = "index2.php?action=manage_products&menu=8&mid="+id;
	}
	return false;	
}

function delete_testimonial(id)
{
	if(confirm("Are you sure you want to delete"))
	{
		document.location = "index2.php?action=manage_testimonial&test_id="+id;
	}
	return false;	
}

function delete_writer_news(id)
{
	if(confirm("Are you sure you want to delete"))
	{
		document.location = "index2.php?action=manage_writer_news&wnews_id="+id;
	}
	return false;	
}

function delete_client_news(id)
{
	if(confirm("Are you sure you want to delete"))
	{
		document.location = "index2.php?action=manage_customer_news&wnews_id="+id;
	}
	return false;	
}

function delete_educational(id)
{
	if(confirm("Are you sure you want to delete"))
	{
		document.location = "index2.php?action=delete_educational&eid="+id;	
	}
	return false;	
}

function delete_id1(id)
{
	if(confirm("Are you sure you want to delete"))
	{
		document.location = "index2.php?action=delete_essay&id="+id;	
	}
	return false;	
}

function delete_video(id)
{
	if(confirm("Are you sure you want to delete"))
	{
		document.location = "index2.php?action=delete_video&vid="+id;
	}
	return false;	
}

function delete_image(id,query,isAdd)
{
	if(confirm("Are you sure you want to delete"))
	{
		if (isAdd == "yes")
		{
			document.location = "delete_image.php?aid="+id+"&"+query;	
		}
		else
		{
			document.location = "delete_image.php?id="+id+"&"+query;	
		}
	}
	return false;	
}

function delete_homeimage(name,query)
{
	if(confirm("Are you sure you want to delete"))
	{
		document.location = "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 echeckName(strEmail) {

		
        var at="@"
        var dot="."
        str=strEmail;
        if(str!="")
       {
        var lat=str.indexOf(at)
        var lstr=str.length
        var ldot=str.indexOf(dot)
        if (str.indexOf(at)==-1){
			alert("Please Enter a Valid Email Address");
           return false
        }

        if (str.indexOf(at)==-1 || str.indexOf(at)==0 || (str.indexOf(at)+1)==lstr){
			alert("Please Enter a Valid Email Address");
			document.getElementById('writer_email').focus();
           return false
        }

        if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || (str.indexOf(dot)+1)==lstr){
			alert("Please Enter a Valid Email Address");
			document.getElementById('writer_email').focus();
            return false
        }

         if (str.indexOf(at,(lat+1))!=-1){
			 alert("Please Enter a Valid Email Address");
			 document.getElementById('writer_email').focus();
            return false
         }

         if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
			 alert("Please Enter a Valid Email Address");
			 document.getElementById('writer_email').focus();
            return false
         }

         if (str.indexOf(dot,(lat+2))==-1){
			 alert("Please Enter a Valid Email Address");
			 document.getElementById('writer_email').focus();
            return false
         }

         if (str.indexOf(" ")!=-1){
			 alert("Please Enter a Valid Email Address");
			 document.getElementById('writer_email').focus();
            return false
         }

         if (str.substring(str.lastIndexOf(dot)+1)==""){
			 alert("Please Enter a Valid Email Address");
			 document.getElementById('writer_email').focus();
            return false
         }

         return true
    }
}
