function trim(s)
{
	return s.replace(/^\s+/g, "").replace(/\s+$/g, "");
}

function cleanRerm(s)
{
    var re = new RegExp(/'$/);
    var re2 = new RegExp(/'\s/);
    s = s.replace(re,"");
    s = s.replace(re2," ");
    s = s.replace("$", "");
    s = trim(s);
    return s;
}

function sendQuery()
{
	var o = document.getElementById("searchTerm");
	var s = o.value;
	s = cleanRerm(s);
	
	if(s == "")
	{
		alert("Please insert search term");
		o.focus();
		return;
	}
	top.document.location.replace("http://archive.globes.co.il/searchgl/" + encodeURI(s.replace(/\//g,  "~~").replace(/& /g, "")) + ".html");
}

function checkKey(evnt, sName)
{
	// checks whether the ENTER key is pressed 
	// If Yes - sendquery
	if (!evnt)
		evnt = window.event;
	var evt = Compat.getEvent(evnt);
    
	if (evt.target.id == "comments")
		return;

	if (evt.keyCode == 13)  
	{
		evt.stopPropagation();
		switch(sName)
		{
	   		case "searchform": sendQuery(); break;
	   		case "shop":	   submitShop();break;	   		
	   		case "thanks":	   goDoc();break;	   		
	   		case "contact":	   submitContactForm();break;
	   	}
	}	
}


function init()
{
	var searchTerm = document.getElementById("searchTerm");
	searchTerm.focus();
}
//////////////////////////shop////////////////////////////////
//Load shop
function initShop()
{
 	loadComboYears("CreditCardExpirationYear");
	document.getElementById("flname").focus();
}

function loadComboYears(cbName)
{
	try{
		var oOption;
		var sDate = new Date();
		var sYear = sDate.getFullYear();
		var shortYear;
		// add first empty line to CB
		oOption = document.createElement("option");
		oOption.text = "Select year";
		oOption.value = "";	
		var oCB = document.getElementById(cbName);
		oCB.options.add(oOption);
			
		// now, lets add next 10 years
		for (var i = 0; i < 10; ++i) {
			
			oOption = document.createElement("option");
			oOption.text = sYear;
			shortYear = new String(sYear);
			oOption.value = shortYear.substr(2, 2);
			oCB.options.add(oOption);
			sYear++;
		}
	}catch(e){alert(e.description);}
}

//validate shop
function checkObligatoryFields(oForm)
{
    
	var oElements = oForm.elements;
	var oElement;
	var sValue;
	for(var i = 0; i < oElements.length; i++)
	{ 
		oElement = oElements[i];
		if(oElement.getAttribute("must") == "yes")
		{
			sValue = getElementValue(oElement);
			if(isEmpty(sValue, oElement))
				return false;
			if(oElement.id == "email")
				if(!isValidEmail(sValue, oElement))
					return false;
			if (oElement.id == "emailConfirm")
			    if (!isMatchEmail(sValue, oElement))
			        return false;
			if(oElement.getAttribute("isnumeric") == "yes")
				if(!checkIsNumeric(oElement))
					return false;
		}
	}
	return true;
}

function getElementValue(o)
{
	switch(o.type)
	{
		case "textarea":
		case "select-one":
		case "text":   return o.value;
		case "radio":  return getSelectedRadio(o);
		default :	   return "";
	}
}

// Email address validation
function isValidEmail(s, o)
{
	if(!(s.search(/^.+@.+\..+/) > -1)) 		// match e-mail pattern
	{
		alert("Email address is invalid");
		o.focus();
		return false;
	}
	return true;
}

function isMatchEmail(s, o)
	{
	    var p = document.getElementById("email");
	    if(s != p.value)
	    {
	        alert("Please correct and try again.\n The email address in the Email address confirmation  is not identical to the address email");
	        o.focus();
	        return false;
	    }
	    return true;    
	}

function isEmpty(s, o)
{
	if(trim(s) == "")
	{
		alert("Please insert " + o.getAttribute("title"));
		o.focus();
		return true;
	}
	return false;
}

function checkIsNumeric(o)
{
	var re = new RegExp("[^0-9]");        
	var creditCard =  document.getElementById("CreditCardNumber").value;   
	if (re.test(o.value))	
	{
		alert("The field " + "[" + o.getAttribute("title") + "]" + " may only contain numeric values");
		o.focus();
		return false;
	}
	if (o.name == "identinum")
	{
		if(o.value.length > 9)
		{
			alert("Maximum length for id field is 9");
			o.focus();
			return false;
		}
	}
	if (o.name == "cvv2")
	{
		if(o.value.length != 4 && o.value.length != 3)
		{
			alert("The field [cvv] may contain 3-4 digits shown in the back of the credit card.");
			o.focus();
			return false;
		}

		if ((o.value == creditCard.substr(creditCard.length - 4)) || (o.value == creditCard.substr(creditCard.length - 3)))
		{
		    if (!confirm("Do you want to continue?\n Please check that you inserted the last 3 digits shown in the back of the credit card and not the last 3 digits of the credit card."))
		    return;
		}
	}
	return true;
}

function getSelectedRadio(o)
{
	var CB = document.getElementsByName(o.name);
	for (var i = 0; i < CB.length; ++i) {
		if (CB.item(i).checked)
			return CB.item(i).value;
	}
	return "";
}

//validate submit form 
var bFirst = true;
function oSubmitShop()
{
    if(!bFirst)
    {
        bFirst = true;
        return false;
    }
    bFirst = false;
    var oForm = document.getElementById("CustDetails");  
    return checkObligatoryFields(oForm);
    if (Compat.isIE)
		bFirst = true;
    return false;
}
//submit shop
function submitShop()
{
	var oForm = document.getElementById("CustDetails");  
    if(!checkObligatoryFields(oForm)) return false;   
	//if (oForm.onsubmit())
    {
		document.getElementById("did").value = did;
		document.getElementById("docTitle").value = docTitle;
		oForm.submit();
     }
}

function goToShop()
{
	var q = new NetisQuery();	
	q.addParam("did", did);
	q.addParam("docTitle", document.getElementById("docTitle").innerText);
	q.submitTo("https://archive.globes.co.il/searchgl/searchgshop.aspx");
}

function isResponseError(oXml)
{
	return oXml.documentElement.tagName.toLowerCase() == "error";
}

function responseContent(oXml)
{
	return oXml.documentElement.text;
}

function openCvv2()
{
	var sUrl = "searchgcvv2.html";
	Compat.openWindow({ url: sUrl, name:"_blank", focus: true },{ width:460, height:450, menubar:"no"});
}

function openWin(sUrl)
{
	window.open(sUrl,"_blank");
}

//submit contact form
//submit shop
function submitContactForm()
{
	var oForm = document.getElementById("searchgContact");
	var oElements = oForm.elements;
	if (checkObligatoryFields(oForm))
	{
		document.getElementById("buttons").style.display = "none";
		document.getElementById("msg").value = document.getElementById("contactContent").innerHTML;
		oForm.submit();
	}
}

//cancel purchase
function cancelPurchase(evt)
{
    evt = Compat.getEvent(evt);
    evt.stopPropagation();
    evt.preventDefault();	

    window.location.replace("http://archive.globes.co.il/");
}

function gotoPurchase(evt)
{
    evt = Compat.getEvent(evt);
    evt.stopPropagation();
    evt.preventDefault();
    
    window.location.replace("http://archive.globes.co.il/default.aspx?action=showCollectionProducts&fromSEO=yes");
}

function displayCVV(bOn, evt)
{
	evt = Compat.getEvent(evt);
	evt.stopPropagation();
	var element = evt.target;
	var newCvv = document.getElementById("newCvv");

	if (bOn)
	    newCvv.style.visibility = "visible";
	else
		newCvv.style.visibility = "hidden";
}
