var p_height;
var p_width;

function imageResize(img,width){
	if(img.width > width){
		img.widht = width;
	}
}

function getImgSize(imgSrc)
{
	/*var newImg = new Image();
	newImg.src = imgSrc;
	p_height = parseInt(newImg.height)+50;
	p_width = newImg.width;
	if( p_width < 350 )
		p_width = 350;
	*/
}
function ValidPhoneNumber(phone)
{
   if(phone.search(/\d{3}\-\d{3}\-\d{4}/)==-1)
   {
      alert("The phone number you entered is not valid.\r\nPlease enter a phone number with the format (Ex: 123-456-7890).");
      return false;
   }
   else
   {
   	return true;
   }
}
function openWindow(pathToOpen) {
	windowMem=window.open(pathToOpen,"memtree","scrollbars=yes,menubar=no,resizable=yes,status=yes");
	windowMem.focus();
	topMargin=0;
	leftMargin=0;
	windowMem.moveTo(topMargin,leftMargin);
	height=screen.height-topMargin;
	width=screen.width-leftMargin;

	if(width > 1024) {
		width=width-0;
		}
	if(height > 768) {
		height=height-25;
		}
	else {
		height=height-25;
		}
	windowMem.resizeTo(width,height);
	}

function openPopup(pathToOpen) {
	
	var w = 1024, h = 768;
	
	/*if (document.all) {
	   alert("MOzilla");	*/
	   /* the following is only available after onLoad */
	   /*w = document.body.clientWidth;
	   h = document.body.clientHeight;
	}*/
	if (document.getElementById || document.all || document.layers) {
	   w = window.innerWidth;
	   h = window.innerHeight;
	}
	
	var popW = 500, popH = 500;
	
	var leftPos = (w-popW)/2, topPos = (h-popH)/2;
	
	window.open(pathToOpen,'popup','width=' + popW + ',height='+popH+',top='+topPos+',left='+leftPos);
/*
	windowMem=window.open(pathToOpen,"memtree","scrollbars=yes,menubar=no,resizable=yes,status=yes");
	windowMem.focus();
	topMargin=100;
	leftMargin=100	;
	windowMem.moveTo(topMargin,leftMargin);
	height=screen.height-topMargin;
	width=screen.width-leftMargin;

	if(width > 1024) {
		width=width-0;
		}
	if(height > 768) {
		height=height-25;
		}
	else {
		height=height-25;
		}
	windowMem.resizeTo(width,height);*/
	}

function filterInput(e) {
	regAllow = (e)?eval(e.allow):eval(event.srcElement.allow);
		if (event.keyCode == 13) { } else {
			if (!String.fromCharCode(event.keyCode).match(regAllow)) event.returnValue=false;
		}
	}

function isValidEmail(emstr)
	{
	var vEMailID=emstr
	var atPos= vEMailID.indexOf("@") + 1
	var lastDotPos= vEMailID.lastIndexOf(".") + 1
	var firstDotPos = vEMailID.indexOf(".") + 1
	var atInstances=0
	var dotInstances=0
	var inValid = new Array()
	var errorMessages = new Array()
	var errNo = 0
		
	for(var i=0;i<vEMailID.length;i++)
		{   
		if(vEMailID.charAt(i)=="@" )
		   atInstances+=1
		else if(vEMailID.charAt(i)==".")
		   dotInstances=1
		}

	inValid[errNo] = (atInstances==0)
	errorMessages[errNo] = "@ symbol is missing"
	errNo++

	inValid[errNo] = (dotInstances==0)
	errorMessages[errNo] = ". symbol is missing"
	errNo++

	inValid[errNo] = (atInstances>1)
	errorMessages[errNo] = "@ symbol should not repeat"
	errNo++

	inValid[errNo] = (Math.abs(firstDotPos-atPos)==1)
	errorMessages[errNo] = "@ and . symbols can't come together"
	errNo++

	inValid[errNo] = ((lastDotPos-atPos)<=1)
	errorMessages[errNo] = ". symbol is missing"
	errNo++

	inValid[errNo] = (atPos==vEMailID.length)
	errorMessages[errNo] = "@ symbol can't appear at end"
	errNo++

	inValid[errNo] = (lastDotPos==vEMailID.length)
	errorMessages[errNo] = ". symbol can't appear at end"
	errNo++

	inValid[errNo] = (atPos==0)
	errorMessages[errNo] = "@ symbol is missing"
	errNo++

	inValid[errNo] = (atPos==1)
	errorMessages[errNo] = "@ symbol should not appear at first position"
	errNo++

	inValid[errNo] = (firstDotPos==0)
	errorMessages[errNo] = ". symbol is missing"
	errNo++

	inValid[errNo] = (firstDotPos==1)
	errorMessages[errNo] = ". symbol should not appear at first position"
	errNo++

	inValid[errNo] = (vEMailID.indexOf(" ")>=0)
	errorMessages[errNo] = "E-mail can't contain blank spaces."
	errNo++

	inValid[errNo] = (vEMailID.indexOf("/")>=0) || (vEMailID.indexOf("\\")>=0)
	errorMessages[errNo] = "E-mail can't contain \\ or /"
	errNo++

	var fullErrorMessage = "Invalid " + name + " email address.\n"
	for(var ind=0; ind<errNo; ind++)
	{
		if(inValid[ind])
		{
			fullErrorMessage += errorMessages[ind] + "\n"
			alert(fullErrorMessage)			
			return false
		}
	}
		var SingleQuotePos = vEMailID.indexOf("'") 
		if(SingleQuotePos!=-1)
		{
					alert("Enter valid characters.Single Quotes ( ' ) are not allowed.")					
					return false

		} 
	return true;
	}


function trim(inputString) 
	{
	if (typeof inputString != "string") 
		{
		return inputString; 
		}
	var retValue = inputString;
	var ch = retValue.substring(0, 1);
	while (ch == " ") 
	{ // Check for spaces at the beginning of the string
		retValue = retValue.substring(1, retValue.length);
		ch = retValue.substring(0, 1);
	}
	ch = retValue.substring(retValue.length-1, retValue.length);
	while (ch == " ") 
	{ // Check for spaces at the end of the string
		retValue = retValue.substring(0, retValue.length-1);
		ch = retValue.substring(retValue.length-1, retValue.length);
	}
	//while (retValue.indexOf(" ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
	//retValue = retVal
	//}
	return retValue;
	}
	

function accountValidation(frm1)
	{ 
	//alert(frm1.email.value); return false;

	
	if(!isValidEmail(frm1.email.value))
		{
		frm1.email.focus();
		return false;
		}

	if(trim(frm1.account_loginname.value).length < 1)
		{
		alert("Login name must be at least 1 characters.");
		frm1.account_loginname.value=trim(frm1.account_loginname.value);
		frm1.account_loginname.focus();
		return false;	
		}

	if(trim(frm1.account_password.value).length < 6)
		{
		alert("Password must be at least 6 characters");
		frm1.account_password.value=trim(frm1.account_password.value);
		frm1.account_password.focus();
		return false;
		}
	
	if(trim(frm1.account_password2.value).length < 6)
		{
		alert("Password must be at least 6 characters");
		frm1.account_password2.value=trim(frm1.account_password2.value);
		frm1.account_password2.focus();
		return false;	
		}		

	if(trim(frm1.account_password.value)!== trim(frm1.account_password2.value))
		{
		alert("Passwords mismatched");
		frm1.account_password.value="";
		frm1.account_password2.value="";
		frm1.account_password.focus();
		return false;	
		}

	if(trim(frm1.aname.value).length < 6)
		{
		alert("Please specify name.");
		frm1.aname.value=trim(frm1.aname.value);
		frm1.aname.focus();
		return false;	
		}

	return true;
	}
	
	function send_email(stremailid,orderId)
	{
		openPopup("sendemail.php?id="+stremailid+"&o="+orderId);
		return;
	}
	
	function setcheckboxvalue(status,hidfieldid)
	{
		if(status)
		{
				document.getElementById(hidfieldid).value=1;
		}
		else
		{
			document.getElementById(hidfieldid).value=0;
		}
		document.frm.submit();
	}
	function popUp(URL,width,height) {
		day = new Date();
		id = day.getTime();
		eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width="+width+",height="+height+",left = 312,top = 134');");
	}
	function show_full_text(){
		document.getElementById('fulltextspn').style.display	=	'block';	
		document.getElementById('partialtextspn').style.display	=	'none';
	}
	function show_partial_text(){
		document.getElementById('partialtextspn').style.display	=	'block';	
		document.getElementById('fulltextspn').style.display	=	'none';
	}
	function delete_mail(id)
	{
		document.getElementById('mailid').value	=	id;
		var conf	=	confirm("Are you sure to delete?");
		if(conf)
		{		
			document.frm.submit();
		}
		else
		{
			return false;	
		}
	}
	function resend_mail(mailId,orderId)
	{
		openPopup("sendemail.php?mailid="+mailId+"&o="+orderId);
		return;
	}

	function checkNumber(textBox){
		//alert(textBox);
		while (textBox.value.length > 0 && isNaN(textBox.value)) {
		textBox.value = textBox.value.substring(0, textBox.value.length - 1);
		}
	textBox.value = trim(textBox.value);
	}
	
	function checkValue(textBox,chkId){
		var curVal = textBox.value;
		var counter = 0;
		if(chkId=='order1'){
			var order = document.frm.order1;
		}else if(chkId=='order2'){
			var order = document.frm.order2;
		}else if(chkId=='order3'){
			var order = document.frm.order3;
		}else{
			var order = document.frm.order4;	
		}
		for(var i=0;i<order.length;i++){
			if(order[i].value!=''){
			if(order[i].value==curVal){
				counter = counter+1;
			 }
			   if(counter>1){
				 alert('This order already exists.');
				textBox.value = textBox.value.substring(0, textBox.value.length - 1);
				textBox.focus();
				return false;
			   }
			}
		}
	}