var isIE = (navigator.appName.indexOf('Microsoft') != -1);

var pImages = new Object();

preloadImage("download_over", "gif");
preloadImage("download_normal", "gif");
preloadImage("download_all_over", "gif");
preloadImage("download_all_normal", "gif");
preloadImage("arabic/download_over", "gif");
preloadImage("arabic/download_normal", "gif");
preloadImage("rollover", "gif");
preloadImage("bg-button01", "gif");
preloadImage("tab_general_n", "gif");
preloadImage("tab_general_o", "gif");
preloadImage("tab_general_s", "gif");
preloadImage("tab_education_n", "gif");
preloadImage("tab_education_o", "gif");
preloadImage("tab_education_s", "gif");
preloadImage("tab_business_n", "gif");
preloadImage("tab_business_o", "gif");
preloadImage("tab_business_s", "gif");

function preloadImage(imageName, imageType){
  pImages[imageName] = new Image();
  pImages[imageName].src = "images/" + imageName + "." + imageType;
}

function replaceDivImage(obj, newImage){
  if (pImages[newImage].width == 0) return;
  obj.style.background = "url(" + pImages[newImage].src + ") no-repeat";
}

function menuover(obj, status){
  if (status){
    if (pImages["rollover"].width){
      obj.style.background = "url(images/rollover.gif)";
    }
    obj.childNodes[1].style.color = "#222222";
    obj.childNodes[1].style.textDecoration = "underline";
  } else {
    obj.childNodes[1].style.color = "#6A6A6A";
    obj.childNodes[1].style.textDecoration = "none";
    obj.style.background = "url(images/bg-button01.gif)";
  }
}

function download(type){
  if (type == undefined) type = 'all';
  window.location.href = "download.php?version=" + type;
}

function check_email(e) {
	var ok = "1234567890qwertyuiop[]asdfghjklzxcvbnm.@-_QWERTYUIOPASDFGHJKLZXCVBNM";
	for(i = 0; i < e.length; i++)
		if (ok.indexOf(e.charAt(i)) < 0)
			return (false);
	if (document.images) {
		re = /(@.*@)|(\.\.)|(^\.)|(^@)|(@$)|(\.$)|(@\.)/;
		re_two = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
		if (!e.match(re) && e.match(re_two))
			return (true);
	}
	return false;
}

function Trim(TRIM_VALUE){
	if(TRIM_VALUE.length < 1){
		return "";
	}
	TRIM_VALUE = RTrim(TRIM_VALUE);
	TRIM_VALUE = LTrim(TRIM_VALUE);
	if(TRIM_VALUE==""){
		return "";
	} else {
		return TRIM_VALUE;
	}
} //End Function

function RTrim(VALUE){
	var w_space = String.fromCharCode(32);
	var v_length = VALUE.length;
	var strTemp = "";
	if(v_length < 0){
		return"";
	}
	var iTemp = v_length -1;
	while(iTemp > -1){
		if(VALUE.charAt(iTemp) != w_space){
			strTemp = VALUE.substring(0,iTemp +1);
			break;
		}
		iTemp = iTemp-1;
	} //End While
	return strTemp;
} //End Function

function LTrim(VALUE){
	var w_space = String.fromCharCode(32);
	if(v_length < 1){
		return"";
	}
	var v_length = VALUE.length;
	var strTemp = "";
	var iTemp = 0;
	while(iTemp < v_length){
		if(VALUE.charAt(iTemp) != w_space){
			strTemp = VALUE.substring(iTemp,v_length);
			break;
		}
		iTemp = iTemp + 1;
	} //End While
	return strTemp;
} //End Function


function createRequestObject() {
    var ro;
    var browser = navigator.appName;
    if(browser == "Microsoft Internet Explorer"){
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    }else{
        ro = new XMLHttpRequest();
    }
    return ro;
}

var http = createRequestObject();

function checkForm(submitType){
  document.getElementById("name_alert").style.display = "none";
  document.getElementById("hwid_alert").style.display = "none";
  var errors = false
  if (Trim(document.forms.purchase.email.value) == ''){
    document.getElementById("name_alert").style.display = "inline";
    errors = true;
  } else if (!check_email(Trim(document.forms.purchase.email.value))){
    document.getElementById("name_alert").style.display = "inline";
    errors = true;
    alert("Please fill a valid email address");
  }
  if (Trim(document.forms.purchase.hwid.value) == ''){
    document.getElementById("hwid_alert").style.display = "inline";
    errors = true;
  }
  if (errors){
    return false;
  }
  // else - ajax:
  document.getElementById("loading_anim").style.display = "inline";
  document.getElementById("submit_button").disabled = true;
  document.getElementById("reset_button").disabled = true;
  var url = "validate_hwid.php?email=" + escape(document.forms.purchase.email.value);
  url += "&hwid="  + escape(document.forms.purchase.hwid.value);
  url += "&submittype=" + submitType;
  http.open('get', url, true);
  http.onreadystatechange = handleResponse;
  http.send(null);
  return false;
}

function isValidKey(evt, validateType){
  var charCode = (evt.which) ? evt.which : evt.keyCode;
  if (evt.which && charCode == 118 && evt.ctrlKey){
    return true;
  }
  if (charCode == 46) return true; // DEL
  if (charCode == 8) return true;  // Backspace
  if (charCode == 9) return true;  // Tab
  if (charCode == 13) return true;  // Enter
  switch (validateType){
    case "name":
      if ((charCode >= 32) && (charCode <= 64)){
      	switch (charCode){
      		case 33: case 34: case 39: case 58: case 59: case 61: return false;
      		default: return true;
      	}
      }
      if ((charCode >= 65) && (charCode <= 90)) return true; // A~Z
      if ((charCode >= 97) && (charCode <= 122)) return true; //a~z
      if (charCode == 45) return true; // -
      return false;
    case "userid":
      if ((charCode >= 65) && (charCode <= 70)) return true; // A~F
      if ((charCode >= 97) && (charCode <= 102)) return true; // a~f
      if ((charCode >= 48) && (charCode <= 57)) return true; // 0~9
      if (charCode == 45) return true; // -
      return false;
    case "ccnumber":
      if ((charCode >= 48) && (charCode <= 57)) return true; // 0~9
      if (charCode == 45) return true; // -
      return false;
  }
}

function resetCardForm(){

}

function resetForm(multy){
  document.getElementById("name_alert").style.display = "none";
  document.getElementById("hwid_alert").style.display = "none";
  document.getElementById("loading_anim").style.display = "none";
  if (multy){
    document.forms.purchase.hwid.value = "";
  }
}

function openWindow(wurl) {
	popupWin = window.open(wurl + ".html" ,'','scrollbars,resizable,toolbar,width=400,height=450,left=50,top=50');
	popupWin.focus();
}

function openResizableWindow(url, l, t, w, h){
	var settings = "menubar=no,";
	settings += "location=no,";
	settings += "scrollbars=no,";
	settings += "status=no,";
	settings += "resizable=yes,";
	settings += "left=" + l + " ,";
	settings += "top=" + t + ",";
	settings += "height=" + h + ",";
	settings += "width=" + w;
	window.open(url, "", settings);
}

function tabOver(tabObj, tabName, mouseStatus){
  if (mouseStatus){
    tabObj.src = pImages["tab_" + tabName + "_o"].src;
  } else {
    tabObj.src = pImages["tab_" + tabName + "_n"].src;
  }
}

function tabClick(tabName){
  window.location.href = window.location.href + "?tab=" + tabName;
}

function writeWatchFlash(){
  divTag = "<div id='watch_this' style='display:none;position:absolute;left:0px;top:0px'>";
  document.write(divTag);
  document.write("<object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000'");
  document.write("codebase='http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0' width='142' height='131'>");
  document.write("<param name='movie' value='images/youmust.swf?theUrl=learntt2.wmv' />");
  document.write("<param name='quality' value='high' />");
  document.write("<param name='wmode'   value='transparent' />");
  embedObj =  "<embed src='images/youmust.swf?theUrl=learntt2.wmv' ";
  embedObj += "quality='high' width='142' height='131' type='application/x-shockwave-flash' ";
  embedObj += "wmode='transparent' pluginspage='http://www.macromedia.com/go/getflashplayer' />";
  document.write(embedObj);
  document.write("</object>");
  document.write("</div>");
  setTimeout(moveWatch, 1000);
}

function moveWatch(){
  divTag = document.getElementById("watch_this");
  divTag.style.left = watchPos[0] + "px";
  divTag.style.top =  watchPos[1] + "px";
  divTag.style.display = '';
}

function writeTestFlash(){
  divTag = "<div align='center' style='margin-top:50px'>";
  document.write(divTag);
  document.write("<object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000'");
  document.write("codebase='http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0' width='600' height='340' align='middle'>");
  document.write("<param name='movie' value='test03.swf' />");
  document.write("<param name='quality' value='high' />");
  document.write("<param name='wmode'   value='transparent' />");
  document.write("<param name='bgcolor' value='#993300' />");
  embedObj =  "<embed src='test03.swf' ";
  embedObj += "quality='high' width='600' height='340' type='application/x-shockwave-flash' ";
  embedObj += "wmode='transparent' pluginspage='http://www.macromedia.com/go/getflashplayer' bgcolor='#993300' />";
  document.write(embedObj);
  document.write("</object>");
  document.write("</div>");
}