var xmlHttp_obj=null;

function GetxmlHttp_object(){
	var xmlHttp=null;
	try{
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}catch (e){
		// Internet Explorer
		try{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}catch (e){
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}

function sendRequest(page,method,formname,divId){
	xmlHttp_obj=GetxmlHttp_object()
	if (xmlHttp_obj==null){
	  alert ("Your browser does not support AJAX!");
	  return false;
	} 
	
	var url=page;
	xmlHttp_obj.onreadystatechange=function(){stateChanged_fnc(divId)};
	//xmlHttp_obj.open("GET",url,true);
	//xmlHttp_obj.send(null);
	
	if(method == "POST"){
		xmlHttp_obj.open('POST',page,true);
		// set form http header
		xmlHttp_obj.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=UTF-8');
		// get form values and send http request
		xmlHttp_obj.send(getFormValues(formname));
	
	
	}else if(method == "GET"){
	
		xmlHttp_obj.open('GET',page,true);
		
		xmlHttp_obj.send(null);						
	}
 
}

function stateChanged_fnc(divId){ 
	if (xmlHttp_obj.readyState==4){
		document.getElementById(divId).value='';
		//alert(xmlHttp_obj.responseText);
	// 	document.getElementById(divId).innerHTML=xmlHttp_obj.responseText;	
		//var sPath = window.location.pathname;
		//var sPage = sPath.substring(sPath.lastIndexOf('\\') + 1);
		//var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
		//alert(sPage);
		
		/*if(sPage=="register.php"){
			window.location="index.php";
		}else 	if(divId=="loginDiv"){
			//window.location.reload();	
			if(sPage=="checkout.php"){
				//alert(sPage);
				window.location.reload();
			}
		}*/
	}
}
function getFormValues(fobjname){
	var fobj=document.getElementById(fobjname);
    var str='';
    for(var i=0;i< fobj.elements.length;i++){
        str+=fobj.elements[i].name+'='+ escape(fobj.elements[i].value)+'&';
    }
    str=str.substr(0,(str.length-1));
	//alert(str); 
    return str;
}

function displayServerResponse(){
	var output = xmlHttp_obj.responseText;	
	document.location.href = output;	
}
function subscribe_enter(e){ 
	 if(window.event) {
	  keynum = event.keyCode;
	 }else if(e.which) {
	  keynum = e.which;
	 }
	 if(keynum==13){
		 submSubscription();
	   return false;
	 } 
}
function blankbox(t){
	if(t.value=="Name" || t.value=="Email"){
		 t.value="";
	}
}
function defaultbox(t,val){
	if(t.value==""){
		t.value=val;
	}
}
function getCookie(c_name){
	if (document.cookie.length>0){
	  c_start=document.cookie.indexOf(c_name + "=")
	  if (c_start!=-1){ 
		c_start=c_start + c_name.length+1 
		c_end=document.cookie.indexOf(";",c_start)
		if (c_end==-1) c_end=document.cookie.length
		return unescape(document.cookie.substring(c_start,c_end))
	  } 
	}
	return ""
}

function setCookie(c_name,value,expiredays){
	var exdate=new Date()
	exdate.setDate(exdate.getDate()+expiredays)
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString())
}

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";
  }
}

function checkCookie(name){
	disclaimer=getCookie(name)
	if (disclaimer !=null && disclaimer !=""){
		return true;
	}else {
		return false;
	}
} 
	
function page_transfer(t,name,url){
	//document.cookie=name+"="+t;
	if(name=="category_id"){
		if(checkCookie("type_id")==true){
			DeleteCookie("type_id");
		}
		if(checkCookie("product_id")==true){
			DeleteCookie("product_id");
		}
		if(checkCookie("category_id")==true){
			DeleteCookie("category_id");
		}
	}
	setCookie(name,t,100);
	if(checkCookie(name)==true){
		window.location=url;
	}else{
		msg = "Please enable Cookies to view this site properly";
	  	alert(msg);	
	}
}

