function ValidateAndDoSearch()
{
    var key = document.getElementById("txtSearchKey").value;
    
    window.location.href = "/Search.aspx?searchtext=" + key;
}

//------------------------------------------------------------------------------------
//Validation for mandatory fields
//------------------------------------------------------------------------------------
function CheckMandatory(MandatoryString)
{
	var Completed = true;
	var sPrefix, oEle;
	
	MandatoryArray = MandatoryString.split("~");	
	
	form_elements:
	for (var i=0; i < MandatoryArray.length; i++)
	{
		sPrefix = MandatoryArray[i].substr(0, 3);
		oEle = document.getElementById(MandatoryArray[i]);
		
		switch (sPrefix)
		{
			case "txt":	if (oEle.value == ''){Completed = false; break form_elements;}break;				
			case "cmb":	if (oEle.selectedIndex <= 0){Completed = false; break form_elements;}break;
		}
	}

	if (Completed == false)
	{
		alert ("Please give information marked as mandatory(*)!");
		return false;
	}
	else
		return true;
}

//------------------------------------------------------------------------------------
//Validation for email addresses
//------------------------------------------------------------------------------------
function emailcheck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert("Invalid E-mail address")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid E-mail address")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		   alert("Invalid E-mail address")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Invalid E-mail address")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Invalid E-mail address")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		   alert("Invalid E-mail address")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Invalid E-mail address")
		    return false
		 }

 		 return true					
}

function ValidateSubscription()
{
    var oForm = document.forms[0];
    
    var dName = document.getElementById("txtName");
    
    var MandatoryString, bValidEmail;
    
    bValidEmail = false;
    
    MandatoryString = "txtName~txtEmail";
    
	if (event.which || event.keyCode)
	{
		if ((event.which == 13) || (event.keyCode == 13))
		{
			return true;
		}
	}
    
    if (trim(dName.value) != 'Name' && (trim(oForm.email.value) != 'E-mail'))
    {
        if (CheckMandatory(MandatoryString) == true)
        {
            if (emailcheck(oForm.email.value) == true)
                bValidEmail = true;
        }
    }
    else
        alert('Please enter a valid Name & an E-mail address.');
    
    return bValidEmail;
}

function ValidateName(oItem)
{
    if (trim(oItem.value) == '' )
        oItem.value = 'Name';
}

function ValidateEmail(oItem)
{
    if (trim(oItem.value) == '' )
        oItem.value = 'E-mail';
}

function ClearName(oItem)
{
    if (trim(oItem.value) == 'Name' )
        oItem.value = '';
}

function ClearEmail(oItem)
{
    if (trim(oItem.value) == 'E-mail' )
        oItem.value = '';
}

function emailPage()
{
    var loc, bdy; 
    
    loc = escape(eval(window.location)).replace(/\+/g, '%2B').replace(/\"/g,'%22').replace(/\'/g, '%27'); 
    bdy = "Dear Friend, One of your friends thought that you might be interested in this.";	
    bdy += " You can view it at, " + loc; 
    bdy += "<br><br>Regards,<br>Your Friend.";
    window.location = "mailto:?subject=:: camtechsystems.co.uk :: &body=" + bdy;
}

function printPage() {if (window.print)	window.print();	else alert("Sorry, your browser doesn't support this feature.");}

function CreateBookmarkLink() 
{ 
	title = document.title;   
	
	url = "http://www.tonertown.co.uk";  
		
	if (window.sidebar) 
	{ 		
		window.sidebar.addPanel(title, url,"");	
	} 
	else if( window.external ) 
	{ 		
		window.external.AddFavorite( url, title); 
	}	
	else if(window.opera && window.print) 
	{ 
		var mbm = document.createElement('a');
		mbm.setAttribute('rel','sidebar');
		mbm.setAttribute('href',url);
		mbm.setAttribute('title',title);
		mbm.click();
	 
	} 
} 

function trim(str, chars) {
    return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}