function trim(str)
{
	var i=0,p = str.length-1;
	while(str.charAt(i)==' ') i++;
	while(str.charAt(p)==' ') p--;
	if(i>p) return '';
	return str.substring(i,p+1);
}
function checkEmail(strng)
{
	var error = "";
	if (strng == "")
		{
		error = "You did not enter an email-address.\n";
		}
	var emailFilter=/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
	if (!(emailFilter.test(strng))) 
		{ 
		error = "Please enter a valid email address.\n";
		}
	var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/;
	if(strng.match(illegalChars))
		{
		error = "The email address contains illegal characters.\n";
		}
	return error;
}
function checkFileName(strng)
{
	var error = "";
	if (strng == "")
		{
		error = "You did not enter a file name.\n";
		}
	var fileNameFilter=/^[a-zA-Z0-9-_]*$/;
	if (!(fileNameFilter.test(strng))) 
		{ 
		error = "Please enter a valid file name.\n ** File name can contain 'alphabets','numbers', 'underscore' or 'hyphen'. \n \n";
		}
	var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/;
	if(strng.match(illegalChars))
		{
		error = "The file name contains illegal characters.\n ** File name can contain 'alphabets','numbers', 'underscore' or 'hyphen'. \n \n";
		}
	return error;
}
function RemoveSpecialCharactersForFileName(strng)
{
	var temp = "";
	var myRegExp = new RegExp("|'|\\\\|\"","g");  
	temp = strng.replace(myRegExp, "-");

	//temp =  strng.replace(/^[a-zA-Z0-9-_]*$/,'_');
	return temp;

}
function checkalphanumeric(alphane)
{
	var str = alphane;
	for(var j=0; j<str.length; j++)
	{
		var alphaa = str.charAt(j);
		var chrcode = alphaa.charCodeAt(0);
		if((chrcode > 47 && chrcode<59) || (chrcode > 64 && chrcode<91) || (chrcode > 96 && chrcode<123) || chrcode==95)
		{
		}
		else
		{
			return false;
		}
	}
	return true;
}
function checkNumeric(strng,errmsgfield) 
{
	var error = "";
	var illegalChars = /\D/;
	if (illegalChars.test(strng)) {
	   	error = errmsgfield+" is invalid.\n";
	}
	return error;
}
function isFloat(strng,errmsgfield) 
{
	var error = "";
	var illegalChars = /^((\d+(\.\d*)?)|((\d*\.)?\d+))$/;
	if (!illegalChars.test(strng)) {
	   	error = errmsgfield+" is invalid.\n";
	}
	return error;
}
function checkEmailAddress(emailval) 
{
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(emailval)){ return true; }
	return false;
}
function checkDateFormat(strngDate)
{
	/* This function will check the date format is (dd/mm/yyyy). If format is not this then it will show error message*/
	// regular expression to match required date format
    
	
	var error = "";
	if (strngDate == "")
	{
		error = "You did not enter date.\n";
	}
	var re = /^(\d{1,2})\/(\d{1,2})\/(\d{4})$/;
	if(!strngDate.match(re))
	{
		error += "The date format is not accepted. Please use the format (dd/mm/yyyy)\n";
	}
    if(strngDate != '')
	{
		if(regs = strngDate.match(re)) 
		{
			if(regs[1] < 1 || regs[1] > 31) 
			{
			  error += "Invalid value for day: " + regs[1] + ".\r\n";
			}
			if(regs[2] < 1 || regs[2] > 12) 
			{
			  error += "Invalid value for month: " + regs[2] + ".\r\n";
			}
			if(regs[3] < 1902 || regs[3] > (new Date()).getFullYear()) 
			{
			  error += "Invalid value for year: " + regs[3] + " - must be between 1902 and " + (new Date()).getFullYear() + ".\r\n";
			}
		} 
    }
	return error;
}
function checkArray(form, arrayName) 
{ 
	var retval = new Array(); 
	for(var i=0; i < form.elements.length; i++) 
	{ 
		var el = form.elements[i]; 
		if(el.type == "checkbox" && el.name == arrayName && el.checked) 
		{
			retval.push(el.value); 
		} 
	} 
	return retval;
}
function sortby(sortname)
{
	var frm = document.formmain;
	if(frm.sortby.value==sortname)
	{
		if(frm.sorttype.value=="ASC")
		{
			frm.sorttype.value="DESC";
		}
		else
		{
			frm.sorttype.value="ASC";
		}
	}
	else
	{
		frm.sortby.value=sortname;
		frm.sorttype.value="ASC";
	}
	frm.submit();
}
function openWin(URL){aWindow=window.open(URL,"theWindow","height=380,width=585,left=200,top=100,scrollbars=no");}
function openWin_TermsConditions(URL){aWindow=window.open(URL,"theWindow","height=380,width=585,left=200,top=100,scrollbars=yes");}
function openWin_EmailFriend(URL){aWindow=window.open(URL,"theWindow","height=600,width=525,left=200,top=100,scrollbars=yes");}
function openWin_PrintPage(URL){aWindow=window.open(URL,"theWindow","height=700,width=535,left=200,top=100,scrollbars=yes");}

function openWin_PhotoLarge_1(URL){aWindow=window.open(URL,"theWindow","height=700,width=640,left=200,top=100,scrollbars=yes");}//only used for product 1
function openWin_PhotoLarge(URL){aWindow=window.open(URL,"theWindow","height=620,width=620,left=200,top=100,scrollbars=yes");}

function openWin_EditGallery(URL){aWindow=window.open(URL,"theWindow","height=600,width=600,left=200,top=100,scrollbars=yes");}
function openWin_ControlPanel(URL){aWindow=window.open(URL,"theWindow","height=580,width=525,left=200,top=100,scrollbars=yes");} // Only used for controlpanel popup
function openWin_OrderPrintPage(URL){aWindow=window.open(URL,"theWindow","height=580,width=600,left=200,top=100,scrollbars=yes");}
function openWin_NewsComments(URL){aWindow=window.open(URL,"theWindow","height=540,width=525,left=200,top=100,scrollbars=yes");}

function openWin_BuyProduct(URL){aWindow=window.open(URL,"theWindow","height=540,width=525,left=200,top=100,scrollbars=yes");}

function changeButtonstatus(DivID_1,DivID_2)
{
	/* Change Button status - Hide the Clicked Button (update,submit etc..) & Show the Processing Button */
	var theElementStyle 	= document.getElementById(DivID_1);
	var theElementStyle2 	= document.getElementById(DivID_2);
	
	if(theElementStyle.style.display == "none")
	{
		theElementStyle.style.display = "block";
		theElementStyle2.style.display = "none";
	
	}
	else
	{
		theElementStyle.style.display = "none";
		theElementStyle2.style.display = "block";
	
	}
}

// Radio Button Validation

function RadioButtonValidation(btn)
{
    var cnt = -1;
    for (var i=btn.length-1; i > -1; i--)
	{
        if (btn[i].checked) 
		{
			cnt = i; i = -1;
		}
    }
    if (cnt > -1) 
		return btn[cnt].value;
    else 
		return null;
}
function imposeMaxLength(Object, MaxLen)
{
//set maximum length to the textarea
  if (Object.value.length > MaxLen)
  		Object.value = Object.value.substring(0, MaxLen);
   // return (Object.value.length <= MaxLen);
}
function AssignValue(source_field_name, destination_field_name, destination_field_name2, destination_field_name3, destination_field_name4)
{
	//alert(document.getElementById(field_name).value);
	if(destination_field_name != "")
	{

	if(document.getElementById(destination_field_name).value == "")
		document.getElementById(destination_field_name).value =document.getElementById(source_field_name).value;
	}
	if(destination_field_name2 != "")
	{
		if(document.getElementById(destination_field_name2).value == "")
		document.getElementById(destination_field_name2).value =document.getElementById(source_field_name).value;
	}
	if(destination_field_name3 != "")
	{
		if(document.getElementById(destination_field_name3).value == "")
		{
			// removes spaces or carrage returns
			//  \s   means any white space (space, carrage return, etc.)
			//  g    means do it globally
			var myExp = /\s/g;
			var myName =  trim(document.getElementById(source_field_name).value);
			var myName = myName.toLowerCase();	
			var myName2 = myName.replace(myExp,"-");
			//alert (myName2)	;
			document.getElementById(destination_field_name3).value = myName2;
		}
	}
	if(destination_field_name4 != "")
	{
	if(document.getElementById(destination_field_name4).value == "")
		document.getElementById(destination_field_name4).value = document.getElementById(source_field_name).value;
	}
}
function AssignTitle(source_field_name, destination_field_name)
{
	//alert(document.getElementById(checkbox_value).value);
	if (document.formmain.chk_page_url.checked == true)
	{
		// removes spaces or carrage returns
		//  \s   means any white space (space, carrage return, etc.)
		//  g    means do it globally
		var myExp = /\s/g;
		var myName =  trim(document.getElementById(source_field_name).value);
		var myName = myName.toLowerCase(); 	
		var myName2 = myName.replace(myExp,"-");
		//alert (myName2)	;
		document.getElementById(destination_field_name).value = myName2;
		//document.getElementById(destination_field_name).value =document.getElementById(source_field_name).value;
	}
	else
	{
		document.getElementById(destination_field_name).value = "";
	}
}

