var newwindow;

function openNew(url, w, h, title, num){
	onSelectedFotoNum = num;
	newwindow = window.open('','newWindows', 'width='+w+', height='+h+', toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, copyhistory=no, resizable=no');
	newwindow.document.clear();
	newwindow.focus();
	newwindow.document.writeln('<html> <head> <title>' + title + '<\/title> <\/head> <body bgcolor= \"#FFFFFF\" style= \"margin:0px; padding:0px\"> <center>');
	newwindow.document.writeln('<a href="javascript:window.close();">');
	newwindow.document.writeln('<img src=' + url + ' title=\"' + title + '\" alt=\"' + title + '\" border=0 >');
	newwindow.document.writeln('</a>');
	newwindow.document.writeln('<\/center> <\/body> <\/html>');
	newwindow.document.close();
	newwindow.focus();
	return false;
}

function openNew2(url, w, h, title, num){
	onSelectedFotoNum = num;
	if(newwindow != undefined)
	{
		newwindow.close();
	}
	newwindow = window.open(url,'newWindows', 'width='+w+', height='+h+', toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, copyhistory=no, resizable=no');
	newwindow.focus();
	return false;
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function $()
{
    var elements = new Array();
    for (var i = 0; i < arguments.length; i++)
    {
        var element = arguments[i];
        if (typeof element == 'string')
            element = document.getElementById(element);
        if (arguments.length == 1)
            return element;
        elements.push(element);
    }
    return elements;
}
function onSearch(){
	var _3="";
	if($("search_txt").value !=="" && $("search_txt").value != "Keyword Search"){
		_3=$("search_txt").value;
		window.location="../search/?search="+escape(_3);
	}else{
		alert("Please enter keyword!");
	}
	return false;
}
function slidedown(obj){
	if(document.getElementById(obj)){
		var o = document.getElementById(obj);
		o.style.display = "block";
	}
}
function slideup(obj){
	if(document.getElementById(obj)){
		var o = document.getElementById(obj);
		o.style.display = "none";
	}
}
function plus(obj, obj2){
	var src = obj.src;
	var s = src.lastIndexOf("/")+1;
	var len = src.length;
	var path = src.substring(0, s);
	var file = src.substring(s, len);
	if(file == "plus.gif"){//minus.gif
		slidedown(obj2);
		obj.src = path+"minus.gif";
	}else{
		slideup(obj2);	
		obj.src = path+"plus.gif";
	}
}
function register(http){
	if(checkAddrForm()){
		var url = "http://www.rpmgarage.com/new/member/register.php";
		var email = document.getElementById("user_email").value; 
		var addr1 = document.getElementById("addr1").value; 
		var addr2 = document.getElementById("addr2").value; 
		var city = document.getElementById("city").value; 
		var zip = document.getElementById("zip").value; 
		var country = document.getElementById("country").value; 
		var sid = document.getElementById("sid").value;
		var first = window.opener.document.getElementById("first").value;
		var last = window.opener.document.getElementById("last").value;
		var phone3 = window.opener.document.getElementById("phone3").value;
		var params = "email="+email+"&phone="+phone+"&phone2="+phone2+"&phone3="+phone3+"&first="+first+"&last="+last+"&addr1="+addr1+"&addr2="+addr2+"&city="+city+"&zip="+zip+"&country="+country+"&sid="+sid;
		
		http.open("POST", url, true);
		http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		http.setRequestHeader("Content-length", params.length);
		http.setRequestHeader("Connection", "close");
		http.onreadystatechange=function(){handleResponse2(http);};
		http.send(params);
	}
}
function handleResponse2(http){
	if(http.readyState == 4 && http.status == 200) {
		document.checkout.submit();
	}
}
function createRequestObject() {
	var A=null; 
	try { 
		A=new ActiveXObject("Msxml2.XMLHTTP") 
	} catch(e) { 
		try { 
			A=new ActiveXObject("Microsoft.XMLHTTP") 
		} catch(oc) { 
			A=null
		}
	} 
	if ( !A && typeof XMLHttpRequest != "undefined" ){ 
		A=new XMLHttpRequest()
	} 
	return A
}
function checkout(title){
	if(title){
		if(checkAddrForm()){
			newwindow = window.open('','newWindows', 'width=460, height=223, toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, copyhistory=no, resizable=no');
			newwindow.document.clear();
			newwindow.focus();
			newwindow.document.writeln('<html> <head> <title>' + title + '<\/title> ');
			newwindow.document.writeln('<script>');
			newwindow.document.writeln('function register(){');
			newwindow.document.writeln('var url = "http://www.rpmgarage.com/new/member/register.php";');
			newwindow.document.writeln('var email = document.getElementById("email").value; ');
			newwindow.document.writeln('var phone = document.getElementById("phone").value; ');
			newwindow.document.writeln('var phone2 = document.getElementById("phone2").value; ');
			newwindow.document.writeln('var first = window.opener.document.getElementById("first").value; ');
			newwindow.document.writeln('var last = window.opener.document.getElementById("last").value; ');
			newwindow.document.writeln('var phone3 = window.opener.document.getElementById("phone3").value; ');
			newwindow.document.writeln('var addr1 = window.opener.document.getElementById("addr1").value; ');
			newwindow.document.writeln('var addr2 = window.opener.document.getElementById("addr2").value; ');
			newwindow.document.writeln('var city = window.opener.document.getElementById("city").value; ');
			newwindow.document.writeln('var zip = window.opener.document.getElementById("zip").value; ');
			newwindow.document.writeln('var country = window.opener.document.getElementById("country").value; ');
			newwindow.document.writeln('var sid = window.opener.document.getElementById("sid").value; ');
			newwindow.document.writeln('var comment = window.opener.document.getElementById("comment").value; ');
			newwindow.document.writeln('var params = "email="+email+"&phone="+phone+"&phone2="+phone2+"&phone3="+phone3+"&first="+first+"&last="+last+"&addr1="+addr1+"&addr2="+addr2+"&city="+city+"&zip="+zip+"&country="+country+"&sid="+sid+"&comment="+comment;');
			newwindow.document.writeln('var orderRef = window.opener.document.getElementById("orderRef").value;');
			newwindow.document.writeln('if(email.length < 6 && email.indexOf("@") < 1 &&  email.indexOf(".") < 1){');
			newwindow.document.writeln('alert("Incorrect Email Address!")');
			newwindow.document.writeln('}else if(phone.length < 3 || phone2.length < 6){');
			newwindow.document.writeln('alert("Incorrect Phone Number!")');
			newwindow.document.writeln('}else{');
			newwindow.document.writeln('window.opener.document.getElementById("orderRef").value = orderRef + "," + email;');
			newwindow.document.writeln('window.opener.document.getElementById("checkout").submit();');
			newwindow.document.writeln('http.open("POST", url, true);');
			newwindow.document.writeln('http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");');
			newwindow.document.writeln('http.setRequestHeader("Content-length", params.length);');
			newwindow.document.writeln('http.setRequestHeader("Connection", "close");');
			newwindow.document.writeln('http.onreadystatechange=function(){handleResponse(http);};');
			newwindow.document.writeln('http.send(params);}');
			newwindow.document.writeln('}');
			newwindow.document.writeln('function handleResponse(http){');
			newwindow.document.writeln('if(http.readyState == 1){');
			newwindow.document.writeln('document.getElementById("status").innerHTML="Connecting...";}');
			newwindow.document.writeln('if(http.readyState == 3){');
			newwindow.document.writeln('document.getElementById("status").innerHTML="Loading...(Do not close this window!)";}');
			newwindow.document.writeln('if(http.readyState == 4 && http.status == 200) {');
	//		newwindow.document.writeln('window.opener.location.reload();');
	//		newwindow.document.writeln('alert(http.responseText)');
			newwindow.document.writeln('window.close();	}}');
			newwindow.document.writeln('function createRequestObject() {');
			newwindow.document.writeln('var A=null; ');
			newwindow.document.writeln('try { ');
			newwindow.document.writeln('A=new ActiveXObject("Msxml2.XMLHTTP") ');
			newwindow.document.writeln('} catch(e) { ');
			newwindow.document.writeln('try { ');
			newwindow.document.writeln('A=new ActiveXObject("Microsoft.XMLHTTP") ');
			newwindow.document.writeln('} catch(oc) { ');
			newwindow.document.writeln('A=null } } ');
			newwindow.document.writeln('if ( !A && typeof XMLHttpRequest != "undefined" ){ ');
			newwindow.document.writeln('A=new XMLHttpRequest() } ');
			newwindow.document.writeln('return A }');
			newwindow.document.writeln('var http=createRequestObject();');
	//		newwindow.document.writeln('function addInputSubmitEvent(form, input) {');
	//		newwindow.document.writeln('	input.onkeydown = function(e) {');
	//        newwindow.document.writeln('		e = e || window.event;');
	//        newwindow.document.writeln('		if (e.keyCode == 13) {');
	//        newwindow.document.writeln('			document.getElementById("form").submit();');
	//        newwindow.document.writeln('			return false;');
	//        newwindow.document.writeln('		}');
	//		newwindow.document.writeln('	};');
	//		newwindow.document.writeln('}');
	//		newwindow.document.writeln('window.onload = function() {');
	//		newwindow.document.writeln('var forms = document.getElementsByTagName("form");');
	//		newwindow.document.writeln('for (var i=0;i < forms.length;i++) {');
	//		newwindow.document.writeln('	var inputs = forms[i].getElementsByTagName("input");');
	//		newwindow.document.writeln('	for (var j=0;j < inputs.length;j++)');
	//		newwindow.document.writeln('		addInputSubmitEvent(forms[i], inputs[j]);');
	//		newwindow.document.writeln('	}');
	//		newwindow.document.writeln('};');
			newwindow.document.writeln('<\/script>');
			newwindow.document.writeln('<\/head> <body style= \"background-image:url(../images/layout/member_email.gif);margin:0px; padding:0px\"> <center>');
			newwindow.document.writeln('<div style="height:70px;"><\/div>');
			newwindow.document.writeln('<div id="status" style="font-family:Verdana, Arial, Helvetica, sans-serif;	font-size:10px;	color:#525760;">Please Enter Your Email Address:<br/>(This will be your login ID and Email to receive tracking number)</div>');
			newwindow.document.writeln('<form id="form" name="form" method="post" action="javascript:register();">');
			newwindow.document.writeln('<input type="text" name="email" id="email" style="width:180px; margin:2px;" /><br />');
			newwindow.document.writeln('<div style="font-family:Verdana, Arial, Helvetica, sans-serif;	font-size:10px;	color:#525760;">Please Enter Your Phone Number</div>');
			newwindow.document.writeln('<input type="text" name="phone" id="phone" style="width:45px; margin:2px;" />');
			newwindow.document.writeln('<input type="text" name="phone2" id="phone2" style="width:125px; margin:2px;" /><br />');
			newwindow.document.writeln('<input type="submit" name="submit" id="submit" value="Submit" style="margin:2px;" /><\/form>');
			newwindow.document.writeln('<\/center> <\/body> <\/html>');
			newwindow.document.close();
			newwindow.focus();
		}
	}else{
		var http=createRequestObject();
		register(http);
	}
}

function bestoffer(tid, offer){
	if(tid){
		newwindow = window.open('','newWindows', 'width=460, height=223, toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, copyhistory=no, resizable=no');
		newwindow.document.clear();
		newwindow.focus();
		newwindow.document.writeln('<html> <head> <title>Member Registration<\/title> ');
		newwindow.document.writeln('<script>');
		newwindow.document.writeln('function register(){');
		newwindow.document.writeln('var url = "http://www.rpmgarage.com/new/member/register3.php";');
		newwindow.document.writeln('var email = document.getElementById("email").value; ');
		newwindow.document.writeln('var phone = document.getElementById("phone").value; ');
		newwindow.document.writeln('var phone2 = document.getElementById("phone2").value; ');
		newwindow.document.writeln('var offer = "'+ offer + '"');
		newwindow.document.writeln('var tid = "' + tid + '"');
		newwindow.document.writeln('var params = "email="+email+"&phone="+phone+"&phone2="+phone2+"&tid="+tid+"&offer="+offer;');
		newwindow.document.writeln('if(email.length < 6 && email.indexOf("@") < 1 &&  email.indexOf(".") < 1){');
		newwindow.document.writeln('alert("Incorrect Email Address!")');
		newwindow.document.writeln('}else if(phone.length < 3 || phone2.length < 6){');
		newwindow.document.writeln('alert("Incorrect Phone Number!")');
		newwindow.document.writeln('}else{');
		newwindow.document.writeln('window.opener.document.getElementById("makeoffer").submit();');
		newwindow.document.writeln('http.open("POST", url, true);');
		newwindow.document.writeln('http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");');
		newwindow.document.writeln('http.setRequestHeader("Content-length", params.length);');
		newwindow.document.writeln('http.setRequestHeader("Connection", "close");');
		newwindow.document.writeln('http.onreadystatechange=function(){handleResponse(http);};');
		newwindow.document.writeln('http.send(params);}');
		newwindow.document.writeln('}');
		newwindow.document.writeln('function handleResponse(http){');
		newwindow.document.writeln('if(http.readyState == 1){');
		newwindow.document.writeln('document.getElementById("status").innerHTML="Connecting...";}');
		newwindow.document.writeln('if(http.readyState == 3){');
		newwindow.document.writeln('document.getElementById("status").innerHTML="Loading...(Do not close this window!)";}');
		newwindow.document.writeln('if(http.readyState == 4 && http.status == 200) {');
//		newwindow.document.writeln('window.opener.location.href = "../detail/?tid=' + tid);
		newwindow.document.writeln('window.close();	}}');
		newwindow.document.writeln('function createRequestObject() {');
		newwindow.document.writeln('var A=null; ');
		newwindow.document.writeln('try { ');
		newwindow.document.writeln('A=new ActiveXObject("Msxml2.XMLHTTP") ');
		newwindow.document.writeln('} catch(e) { ');
		newwindow.document.writeln('try { ');
		newwindow.document.writeln('A=new ActiveXObject("Microsoft.XMLHTTP") ');
		newwindow.document.writeln('} catch(oc) { ');
		newwindow.document.writeln('A=null } } ');
		newwindow.document.writeln('if ( !A && typeof XMLHttpRequest != "undefined" ){ ');
		newwindow.document.writeln('A=new XMLHttpRequest() } ');
		newwindow.document.writeln('return A }');
		newwindow.document.writeln('var http=createRequestObject();');
//		newwindow.document.writeln('function addInputSubmitEvent(form, input) {');
//		newwindow.document.writeln('	input.onkeydown = function(e) {');
//        newwindow.document.writeln('		e = e || window.event;');
//        newwindow.document.writeln('		if (e.keyCode == 13) {');
//        newwindow.document.writeln('			document.getElementById("form").submit();');
//        newwindow.document.writeln('			return false;');
//        newwindow.document.writeln('		}');
//		newwindow.document.writeln('	};');
//		newwindow.document.writeln('}');
//		newwindow.document.writeln('window.onload = function() {');
//		newwindow.document.writeln('var forms = document.getElementsByTagName("form");');
//		newwindow.document.writeln('for (var i=0;i < forms.length;i++) {');
//		newwindow.document.writeln('	var inputs = forms[i].getElementsByTagName("input");');
//		newwindow.document.writeln('	for (var j=0;j < inputs.length;j++)');
//		newwindow.document.writeln('		addInputSubmitEvent(forms[i], inputs[j]);');
//		newwindow.document.writeln('	}');
//		newwindow.document.writeln('};');
		newwindow.document.writeln('<\/script>');
		newwindow.document.writeln('<\/head> <body style= \"background-image:url(../images/layout/member_email.gif);margin:0px; padding:0px\"> <center>');
		newwindow.document.writeln('<div style="height:70px;"><\/div>');
		newwindow.document.writeln('<div id="status" style="font-family:Verdana, Arial, Helvetica, sans-serif;	font-size:10px;	color:#525760;">Please Enter Your Email Address:<br/>(This will be your login ID and Email to receive tracking number)</div>');
		newwindow.document.writeln('<form id="form" name="form" method="post" action="javascript:register();">');
		newwindow.document.writeln('<input type="text" name="email" id="email" style="width:180px; margin:2px;" /><br />');
		newwindow.document.writeln('<div style="font-family:Verdana, Arial, Helvetica, sans-serif;	font-size:10px;	color:#525760;">Please Enter Your Phone Number</div>');
		newwindow.document.writeln('<input type="text" name="phone" id="phone" style="width:45px; margin:2px;" />');
		newwindow.document.writeln('<input type="text" name="phone2" id="phone2" style="width:125px; margin:2px;" /><br />');
		newwindow.document.writeln('<input type="submit" name="submit" id="submit" value="Submit" style="margin:2px;" /><\/form>');
		newwindow.document.writeln('<\/center> <\/body> <\/html>');
		newwindow.document.close();
		newwindow.focus();
	}else{
		var http=createRequestObject();
		register(http);
	}
}


function ems(){
	newwindow = window.open('','newWindows', 'width=460, height=292, toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, copyhistory=no, resizable=no');
	newwindow.document.clear();
	newwindow.focus();
	newwindow.document.writeln('<html> <head> <title>EMS<\/title> ');
	newwindow.document.writeln('<\/head> <body style= \"background-image:url(../images/layout/blank_bg.gif);margin:0px; padding:0px\"> <center>');
	newwindow.document.writeln('<table width="75%" height="100%" border="0" cellspacing="0" cellpadding="0"><tr><td>');
	newwindow.document.writeln('<table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td>');
	newwindow.document.writeln('<tr><td><div style="width:75%; font-size:12px;font-family:Verdana, Arial, Helvetica, sans-serif; font-weight:bold; text-align:left">EMS<\/div><\/td><\/tr>');
newwindow.document.writeln('<tr><td><tr><td><div style="font-size:12px; font-family:Verdana, Arial, Helvetica, sans-serif;">Post office EMS Express service, Usually take 3~5 days business after it is shipped<\/td><\/tr><\/table><\/td><\/tr><\/table>');
	newwindow.document.writeln('<\/center> <\/body> <\/html>');
	newwindow.document.close();
	newwindow.focus();
}

function air(){
	newwindow = window.open('','newWindows', 'width=460, height=292, toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, copyhistory=no, resizable=no');
	newwindow.document.clear();
	newwindow.focus();
	newwindow.document.writeln('<html> <head> <title>EMS<\/title> ');
	newwindow.document.writeln('<\/head> <body style= \"background-image:url(../images/layout/blank_bg.gif);margin:0px; padding:0px\"> <center>');
	newwindow.document.writeln('<table width="75%" height="100%" border="0" cellspacing="0" cellpadding="0"><tr><td>');
	newwindow.document.writeln('<table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td>');
	newwindow.document.writeln('<tr><td><div style="width:75%; font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px; font-weight:bold; text-align:left">Air Mail<\/div><\/td><\/tr>');
	newwindow.document.writeln('<tr><td><tr><td><div style="font-size:12px; font-family:Verdana, Arial, Helvetica, sans-serif;">Post office Air Mail service, Usually take 7~20 days business after it is shipped<\/td><\/tr><\/table><\/td><\/tr><\/table>');
	newwindow.document.writeln('<\/center> <\/body> <\/html>');
	newwindow.document.close();
	newwindow.focus();
}
function checkquickform(form){
	var b = form.brand.value;
	var m = form.model.value;
	var c = form.type.value;
	var msg = "";
	if(b == "all"){
		msg += "Brand";
	}
	if(m == "all"){
		if(msg) msg += ", ";
		msg += "Model";
	}
	if(c == "all"){
		if(msg) msg += ", ";
		msg += "Category";
	}
	if(msg){
		alert("Please select "+msg);
		return false;	
	}
	return true;
}
function changeSubmit(obj){
	var form = document.getElementById("checkout");
	if(obj.id == "method2"){
		form.action = "https://www.paydollar.com/b2c2/eng/dPayment/payComp.jsp";
	}else if(obj.id == "method"){
		form.action = "https://www.paypal.com/cgi-bin/webscr";
	}
}
function checkAddrForm(){
	var msg = "";
	if($("first").value == ""){
		msg += "Please Enter First Name!\n\r";
	}
	if($("last").value == ""){
		msg += "Please Enter Last Name!\n\r";
	}
	if($("email").value == "" || !isEmail($("email").value)){
		msg += "Please Enter Correct Email Address!\n\r";
	}
	if($("addr1").value == ""){
		msg += "Please Enter Address!\n\r";
	}
	if($("addr2").value == ""){
		msg += "Please Enter Address Line2!\n\r";
	}
	if($("city").value == ""){
		msg += "Please Enter City!\n\r";
	}
	if($("phone3").value < 1){
		msg += "Please Enter Phone Number!\n\r";
	}
	if($("country").value < 1){
		msg += "Please Select Country!\n\r";
	}
	if(msg){
		alert(msg);
		return false;
	}else{
		return true;
	}
	return false;
}
function isWhole (s) {
	var isWhole_re       = /^\s*\d+\s*$/; 
	return String(s).search (isWhole_re) != -1
}
function changeAddrForm(){
	if($("addr1").disabled){
		$("addr1").disabled = false;
		$("addr2").disabled = false;
		$("city").disabled = false;
		$("zip").disabled = false;
		$("country").disabled = false;
	}
}
function isEmail (s) {
	var isEmail_re       = /^\s*[\w\-\+_]+(\.[\w\-\+_]+)*\@[\w\-\+_]+\.[\w\-\+_]+(\.[\w\-\+_]+)*\s*$/;
	return String(s).search (isEmail_re) != -1;
}