//setInterval(checkmailwindow, "200");
var childWin = null;
function checkmailwindow()
{
	if (childWin && childWin.document.success) proceed(true);
}
function popup(url, w, h)
{
    window.open("img/" + url, "_blank", "left=100,top=100,width=" + (w + 15) + ",height=" + (h + 15));
}
function upgradeWin(pName, nameTo, nameFrom)
{
	window.open("http://totalrecorder.com/upgradeinfo.htm?payment=" + escape(allprices[pName][0]) + "&nameTo=" + nameTo+"&nameFrom=" + nameFrom, "_blank", "left=100,top=100,width=600,height=400");
}
function showprice()
{
	document.getElementById("someprice").innerHTML = arr[document.forms[0].otherproduct.value];
}
function clickon(id)
{
    var f = document.forms[0];
    if (f.product)
    {
        if (f.product.value != id)
        {
            for (var i = 1; i < 11; i++)
            {
                document.getElementById("purc" + i).className = (i == id?"s":"u") + "txt";
                document.getElementById("ed" + i).style.display = (i == id?"":"none");
            }
	    
	    //if (id == "4") showprice();
        }
    }
}
function proceed(force)
{
	if (force) childWin = null;
	var fr = document.forms[0];
	var i = 0;
	var product = null;
	var pmethod = null;
	
	var strUpgradeFrom  = null;
	var strUpgradeTo	= null;
	
	while (fr.product[i])
	{
		if (fr.product[i].checked)
		{
			//alert("Product code: " + fr.product[i].value);
			product = fr.product[i].value;
		}
		i++;
	}
	if (!product)
	{
		alert("Please select a product!");
		return;
	}
	
	i = 0;
	while (fr.purchasemethod[i])
	{
		if (fr.purchasemethod[i].checked)
		{
			//alert("Purchase method: " + fr.purchasemethod[i].value);
			pmethod = fr.purchasemethod[i].value;
		}
		i++;
	}
	
	if ((product == "trpeup" || product == "trdeup" || product == "trvetodeup" || product == "trsetoveup" || product == "trpetoveup") && !force)
	{
// Initialize nameFrom and nameTo text
		if (product == "trpeup")
		{
			strUpgradeFrom = "Total Recorder Standard Edition";
			strUpgradeTo = "Total Recorder Professional Edition";
 		}
		else if (product == "trdeup")
		{
			strUpgradeFrom = "Total Recorder Professional Edition";
			strUpgradeTo = "Total Recorder Developer Edition";
 		}
		else if (product == "trvetodeup")
		{
			strUpgradeFrom = "Total Recorder VideoPro Edition or Video Add-on";
			strUpgradeTo = "Total Recorder Developer Edition";
  		}
		else if (product == "trsetoveup")
		{
			strUpgradeFrom = "Total Recorder Standard Edition";
			strUpgradeTo = "Total Recorder VideoPro Edition";
  		}
 		else if (product == "trpetoveup")
		{
			strUpgradeFrom = "Total Recorder Professional Edition";
			strUpgradeTo = "Total Recorder VideoPro Edition";
 		}
 
		
		childWin = window.open("../UpgradeTR/checkemail.htm?product=" + allprices[product][4] + "&id=" + product + "&payment=" + escape(allprices[product][0]) + "&nameTo=" + strUpgradeTo + "&nameFrom=" + strUpgradeFrom, "_blank", "left=100,top=100,width=600,height=400");
		return false;
	}
	
	var href;
	
	if (pmethod != null)
	{
		if (pmethod == "1" || pmethod == 1)
		{
			var bycard = document.forms[1];
			var bycardID = document.getElementById("bycard");
			var usd = fr.creditusd[0].checked;
			
			var field = document.createElement("input");
			field.type = "hidden";
			field.name = usd?"BUS12":"BCAN18";
			field.value = "      " + (usd?allprices[product][0]:allprices[product][1]) + " " + (usd?"US":"Canadian") + " Dollars    ";
			bycardID.appendChild(field);
			bycard.Products.value = allprices[product][2] + (usd?"us":"can") + (product == "tr"?(usd?"12":"18"):"") + ",1";
			//bycard.Products.value = "TESTus" + ",1";
			
			if (parseInt(navigator.appVersion) >= 4)
			{
				bycard.last_visit_ref.value = getCookieEx('LAST_VISIT_REF');
				bycard.first_affl_visit.value = getCookie('FIRST_AFFL_VISIT');
			}
			
			/*bycard.product.value = product;
			bycard.pmethod.value = pmethod;*/
			
			bycard.submit();
			return false;
		}
		else if (pmethod == "2")
		{
			window.location.href = "http://totalrecorder.com/purchasebyphone.htm?id=" + allprices[product][3] + "&price=" + allprices[product][0] + "&name=" + escape(allprices[product][4]) +
			"&product=" + product + "&pmethod=" + pmethod;
			return true;
		}
		else if (pmethod == "3")
		{
			var alternate = document.forms[2];
			alternate.productid.value = allprices[product][3];
			alternate.language.value = fr.alternatelanguage.value;
			alternate.submit();
			return false;
		}
		else if (pmethod == "4")
		{
			window.location.href = "http://totalrecorder.com/purchasebycheck.htm?name=" + escape(allprices[product][4]) + "&usd=" + allprices[product][0] + "&cad=" + allprices[product][1] + "&cadgst=" + allprices[product][5] +
			"&product=" + product + "&pmethod=" + pmethod;
			return true;
		}
	}
}
function openWin(url)
{
	window.open(url, "_blank", "left=100,top=100,width=600,height=500");
}

	var caution = false
        var HC_DOMAIN = "highcriteria.com"
        var TR_DOMAIN = "totalrecorder.com"


	// name - name of the cookie
	// value - value of the cookie
	// [expires] - expiration date of the cookie
	// (defaults to end of current session)
	// [path] - path for which the cookie is valid
	// (defaults to path of calling document)
	// [domain] - domain for which the cookie is valid
	// (defaults to domain of calling document)
	// [secure] - Boolean value indicating if
	// the cookie transmission requires a secure transmission
	// * an argument defaults when it is assigned null as a placeholder
	// * a null placeholder is not required for trailing omitted arguments

  function setCookie(name, value, expires, path, domain, secure) {
    var curCookie = name + "=" + escape(value) +
                    ((expires) ? "; expires=" + expires.toGMTString() : "") +
                    ((path) ? "; path=" + path : "") +
                    ((domain) ? "; domain=" + domain : "") +
                    ((secure) ? "; secure" : "")
    if (!caution || (name + "=" + escape(value)).length <= 4000)
      document.cookie = curCookie
    else
      if (confirm("Cookie exceeds 4KB and will be cut!"))
        document.cookie = curCookie
  }

  // name - name of the cookie
  // * return string containing value
  // of specified cookie or null if cookie
  // does not exist

  function getCookie(name) {
    var prefix = name + "="
    var cookieStartIndex = document.cookie.indexOf(prefix)
    if (cookieStartIndex == -1)
      return null
    var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex +
                          prefix.length)
    if (cookieEndIndex == -1)
      cookieEndIndex = document.cookie.length
    return unescape(document.cookie.substring(cookieStartIndex +
            prefix.length,
            cookieEndIndex))
  }

  // name - name of the cookie
  // [path] - path of the cookie
  // (must be same as path used to create cookie)
  // [domain] - domain of the cookie
  // (must be same as domain used to create cookie)
  // * path and domain default if assigned
  // null or omitted if no explicit argument proceeds

  function deleteCookie(name, path, domain) {
    if (getCookie(name)) {
      document.cookie = name + "=" +
                        ((path) ? "; path=" + path : "") +
                        ((domain) ? "; domain=" + domain : "") +
                        "; expires=Thu, 01-Jan-70 00:00:01 GMT"
    }
  }


  // date - any instance of the Date object
  // * you should hand all instances of the
  // Date object to this function for "repairs"
  // * this function is taken from
  // Chapter 14, "Time and Date in JavaScript", in
  // "Learn Advanced JavaScript Programming"

  function fixDate(date) {
    var base = new Date(0)
    var skew = base.getTime()
    if (skew > 0)
      date.setTime(date.getTime() - skew)
  } 
  
  //
  // setCookBoth() - save cookie for both domain: "www.<host>.com" and "<host>.com"
  // 

  function setCookBoth(name, value, expires, path) {
    var dom = null

    if (location.href.indexOf(HC_DOMAIN, 0) >= 0) 
      if (location.href.indexOf("www."+HC_DOMAIN, 0) >= 0) 
        dom = HC_DOMAIN
      else
        dom = "www."+HC_DOMAIN
    else if (location.href.indexOf(TR_DOMAIN, 0) >= 0) 
      if (location.href.indexOf("www."+TR_DOMAIN, 0) >= 0) 
        dom = TR_DOMAIN
      else
        dom = "www."+TR_DOMAIN
    else
      dom = null

    setCookie(name, value, expires, path)
    if (dom != null)
      setCookie(name, value, expires, path, dom)
  }

//
  // renewRefer() - save REFERRAL PAGE in cookie expitarion term is 30 days.
  //                If there is no REFERRAL PAGE and no value saved before
  //                the valie 'NO_REF_PAGE' will be saved.
  // 

  function renewRefer() {
    var REF_NAME = "LAST_VISIT_REF"
    var ref = document.referrer.toLowerCase()
    var now = new Date()
    fixDate(now)
    now.setTime(now.getTime() + 30 * 24 * 60 * 60 * 1000)
    if (ref) { // REFERRAL PAGE exists
      // Save it if it is not from our domain
      if ((ref.indexOf(HC_DOMAIN) == -1)  &&
          (ref.indexOf(TR_DOMAIN) == -1) &&
          (ref.indexOf("test.test.com") == -1)) {
        setCookBoth(REF_NAME, ref, now, "/")
      }
    }
    else {    // REFERRAL PAGE does not exist
      // Save the value 'NO_REF_PAGE' if no value saved before
      if (!getCookie(REF_NAME))
        setCookBoth(REF_NAME, "NO_REF_PAGE", now, "/")
    }
  }


  //
  // getCookieEx() - get cookie with checking if cookies enabled.
  //            Returns:
  //            - value               - if getCookie() sccessful
  //            - null                - if getCookie() return 'null'
  //            - 'COOKIE_DISABLED'   - if setCookie() is failed
  // 

  function getCookieEx(name) {
    var TMP_NAME = "TEMP_TEST_COOKIE"
    var res = getCookie(name)
    if (!res) {
      // Try to save a temporary cookie and read it again
      setCookie(TMP_NAME, "<TEMP_TEST_COOKIE'S_VALUE>")
      if (!getCookie(TMP_NAME)) {
        // Cookie disabled
        res = "COOKIE_DISABLED"
      }
    }
    return res
  }
  function getParam(name) {
    var prefix = name + "="
    var paramStartIndex = location.search.indexOf(prefix)
    if (paramStartIndex == -1)
      return null
    var paramEndIndex = location.search.indexOf("&", paramStartIndex +
                          prefix.length)
    if (paramEndIndex == -1)
      paramEndIndex = location.search.length
    return unescape(location.search.substring(paramStartIndex +
            prefix.length,
            paramEndIndex))
  }
  function changeProduct(obj)
  {
	if (document.getElementById("finalproduct") && obj && obj.checked)
		document.getElementById("finalproduct").innerHTML = allprices[obj.value][4];
	changeText(obj.value, determinePurchase());
  }
  function startup()
  {
	renewRefer();
	var param = getParam("product");
	if (param == null || param == "") param = "trpe";
	{
		for (var i = 0; i < document.forms[0].product.length;i++)
		{
			if (document.forms[0].product[i].value == param)
			{
				document.forms[0].product[i].checked = true;
				if (document.getElementById("finalproduct"))
					document.getElementById("finalproduct").innerHTML = allprices[param][4];
			}
		}
	}
	param = getParam("pmethod");
	if (param == null || param == "") param = "1";
	{
		for (var i = 0; i < document.forms[0].purchasemethod.length;i++)
		{
			if (document.forms[0].purchasemethod[i].value == param)
			{
				document.forms[0].purchasemethod[i].checked = true;
				showText(parseInt(param));
			}
		}
	}
  }
function changeText(product, pmethod)
{
	if (product && pmethod)
	{
		var letter = ((product == "trpeup" || product == "trsetoveup" || product == "trpetoveup" || product == "trvetodeup")?"A":"B");
		var blockId = "purchase" + pmethod + "text" + letter;
		var block = document.getElementById(blockId);
		if (block)
		{
			block.style.display = "";
			letter = letter == "A"?"B":"A";
			block = document.getElementById("purchase" + pmethod + "text" + letter);
			if (block) block.style.display = "none";
		}
	}
}
function showText(id)
{
	for (var i = 1; i < 5; i++)
	{
		var doc = document.getElementById("purchase" + i);
		doc.style.display = id == i?"":"none";
	}
	changeText(determineProduct(), id);
}
function determinePurchase()
{
	for (var i = 0; i < document.forms[0].purchasemethod.length;i++)
	{
		if (document.forms[0].purchasemethod[i].checked) return document.forms[0].purchasemethod[i].value;
	}
	return null;
}
function determineProduct()
{
	for (var i = 0; i < document.forms[0].product.length;i++)
	{
		if (document.forms[0].product[i].checked) return document.forms[0].product[i].value;
	}
	return null;
}
var req = null;
function processReqChange()
{
	if (req && req.readyState == 4 /*&& req.status == 200*/)
	{
		var i = req.responseText.indexOf("success");
		if (req.responseText && i != -1)
		{
			proceed(true);
		}
		else alert("Your registration record is not found in our database.\nVerify product name and email address. The email address should be the same one you have used when this product was registered.");
	}
}
function checkit(url, id, email)
{
	var addon = "?id=" + id + "&Email=" + escape(email);
    if (window.XMLHttpRequest)
    {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChange;
        req.open("GET", url + addon, true);
        req.send(null);
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChange;
            req.open("GET", url + addon, true);
            req.send();
        }
    }
    return false;
}