var currentTextboxId = '';
var callValidate = false;
var designerNameTextboxId;
var triggerButton;


var matchCssClassRegEx = new RegExp('(^|\\s+)listbox-trigger($|\\s+)','i');
(function()
{
	window.onload = function()
	{
		document.body.onclick = function(e)
		{
			if(!e)
				e = window.event;
			var tagName;
			var elm;
			if(typeof e.target != 'undefined')
			{
				elm = e.target;
				tagName = e.target.tagName;
			}
			else
			{
				elm = e.srcElement;
				tagName = e.srcElement.tagName;
			}
			if(!matchCssClassRegEx.test(elm.className))
			{
				if(typeof currentlyOpenListboxes != 'undefined' && currentlyOpenListboxes.length > 0)
				{
					for(var currentListboxID = 0; currentListboxID < currentlyOpenListboxes.length; currentListboxID++)
						hideListbox(currentlyOpenListboxes[currentListboxID]);				
				}
			}
		}
		
		document.body.onkeypress = function(e)
		{
			if(!e)
				e = window.event;
			var charCode;
			if(e.keyCode)
				charCode = e.keyCode;
			else if(e.which)
				charCode = e.which;
			if(typeof charCode != 'undefined')
			{
				if(charCode == 13)
				{
					if(typeof triggerButton != 'undefined')
						document.getElementById(triggerButton).onclick();
				}
			}
			else
				alert('Could not get the key code.');
			
		}
	}
})();


function setCurrentOwnerTextboxId(textboxId)
{
	currentTextboxId = textboxId;
}

function WBC_SetHLink(strURL)
{ 
	document.getElementById(currentTextboxId).value = strURL;
}

function WBC_GetHLink()
{ 
	return document.getElementById(currentTextboxId).value;
}

function popup(url, title, params, textboxID)
{
	debugger;
	var furl = decodeURI(url) + document.getElementById(textboxID).value;
	window.open(furl, title, params)
}

function loadFile(filename, filetype)
{	
	var ref;
	if (filetype=="text/javascript")
	{ 
		ref = document.createElement('script');
		ref.setAttribute("type","text/javascript");
		ref.setAttribute("src", filename);
	}
	else if (filetype=="text/css")
	{ 
		ref =document.createElement("link");
		ref.setAttribute("rel", "stylesheet");
		ref.setAttribute("type", "text/css");
		ref.setAttribute("href", filename);
	}
	if (typeof ref != "undefined")
		document.getElementsByTagName("head")[0].appendChild(ref);
}

function redirect(url, textboxId)
{
	if(textboxId)
	{
		var queryParam = document.getElementById(textboxId).value;
		location.href = encodeURI(url + queryParam);
	}
	else
		location.href = url;
}


function validateTextbox(id, type, message, defaultText)
{
	var textValue;
	var elm = document.getElementById(id);
	switch(type.toUpperCase())
	{
		case 'REQUIRED' : 
			textValue = elm.value;
			if((textValue == defaultText) || (textValue == null) || (textValue.length == 0) || /^\s+$/.test(textValue))
			{	
				alert(message); 
				elm.value = '';
				elm.focus();
				return false; 
			}
			break;
		case 'MAILID' :
			textValue = elm.value;
			if(textValue == defaultText || textValue == '')
			{
				/*alert('Please enter your email address');*/
                                alert(message);
				elm.value = '';
				elm.focus();
				return false; 
			}
			else if (!(/\w{1,}[@][\w\-]{1,}([.]([\w\-]{1,})){1,3}$/.test(textValue)))
			{
				alert(message);
				elm.focus();
				return false;  
			}
			break;
	}	
	return true; 
}


function postToFacebookProfile()
{
	u=location.href;
	t=document.title;
	window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
	return false;
}


/* This method is used to open a search tips page*/
function RedirectToTipsPage(path)
{
 window.open(path);
 return false;
}

function toggleCheck(sender, checkboxID)
{
	var checkbox = document.getElementById(checkboxID);
	checkbox.checked = !sender.checked;
}

function interceptOnSubmtAndValidateTextbox(textboxId, errorMessage)
{
	document.forms[0].onsubmit = function()
	{
		var textbox = document.getElementById(textboxId);
		if(textbox.value == '')
		{
			alert(errorMessage);
			textbox.focus();
			return false;
		}
		return true;
	}
}

function call(id)
{
	var activeHTMLPlaceholder = document.getElementById(id).getElementsByTagName('object')[0];
}

function pageValidate()
{
	if(callValidate)
		return validateTextbox(designerNameTextboxId, 'REQUIRED', 'Please enter the designer name', '');
	else
		return true;
}



var yourname,youremail ,friendname,friendemail,emailvalidation;
function isWhitespace(charToCheck) {
	//var whitespaceChars = "/^\s+$/";
	//return (whitespaceChars.test(charToCheck));
	 var re = /^[^\s()<>@,;:\/]+@\w[\w\.-]+\.[a-z]{2,}$/i
  return re.test(charToCheck);

}


function CheckFriends()
{
	var m,n,n1,m2;
	m=document.forms[0].elements['TxtFriendEmail'].value;
	m2=document.forms[0].elements['TxtYourEmail'].value;
	n=document.forms[0].elements['TxtFriendName'].value;
	n1=document.forms[0].elements['TxtYourName'].value;
	if ((n1 == '') || (n1 == ' ')) { 
	alert(yourname+"\n");document.forms[0].elements['TxtYourName'].focus();return false; }
	if ((m2 == '') || (m2 == ' ')) { 
	alert(youremail+"\n");document.forms[0].elements['TxtYourEmail'].focus();return false; }
	
	var a1 = 1;
	var ml1 = m2.length;

	while ((ml1>a1) && (m2.charAt(a1) != "@")) { a1++ }
	if ((a1 >= ml1) || (m2.charAt(a1) != "@")) {
	alert(emailvalidation+"\n");
	document.forms[0].elements['TxtYourEmail'].focus();
	return false;
	} else {
	a1 = a1 + 2;
	}
	while ((ml1 > a1) && (m2.charAt(a1) != ".")) { a1++ }
	if ((a1 >= ml1 - 1) || (m2.charAt(a1) != ".")) {
	alert(emailvalidation+"\n");
	document.forms[0].elements['TxtYourEmail'].focus();
	return false;
	} 
	if (!isWhitespace(m2)){
	alert(emailvalidation+"\n");
	document.forms[0].elements['TxtYourEmail'].focus();
	return false;
	} 	
	
	
	
	if ((n == '') || (n == ' ')) { 
	alert(friendname+"\n");document.forms[0].elements['TxtFriendName'].focus();return false; }
	if ((m == '') || (m == ' ')) { 
	alert(friendemail+"\n");document.forms[0].elements['TxtFriendEmail'].focus();return false; }
	

	var a = 1;
	var ml = m.length;

	while ((ml>a) && (m.charAt(a) != "@")) { a++ }
	if ((a >= ml) || (m.charAt(a) != "@")) {
		alert(emailvalidation+"\n");
		document.forms[0].elements['TxtFriendEmail'].focus();
		return false;
	} 
	else 
	{
		a = a + 2;
	}
	while ((ml > a) && (m.charAt(a) != ".")) { a++ }
	if ((a >= ml - 1) || (m.charAt(a) != ".")) {
		alert(emailvalidation+"\n");
		document.forms[0].elements['TxtFriendEmail'].focus();
		return false;
	}	
	
	if (!isWhitespace(m)){
	alert(emailvalidation+"\n");
	document.forms[0].elements['TxtFriendEmail'].focus();
	return false;
	} 	
	else
	{
	return true;	
	}
	
}


