var ns4 = document.layers;
var op5 = (navigator.userAgent.indexOf("Opera 5")!=-1)||(navigator.userAgent.indexOf("Opera/5")!=-1);
var op6 = (navigator.userAgent.indexOf("Opera 6")!=-1)||(navigator.userAgent.indexOf("Opera/6")!=-1);
var agt=navigator.userAgent.toLowerCase();
var mac = (agt.indexOf("mac")!=-1);
var ie = (agt.indexOf("msie") != -1); 
var mac_ie = mac && ie;

var imageWin = null;

function getRealLeft(el) {
    xPos = el.offsetLeft;
    tempEl = el.offsetParent;
    while (tempEl != null) {
        xPos += tempEl.offsetLeft;
        tempEl = tempEl.offsetParent;
    }
    return xPos;
}

function getRealTop(el) {
    yPos = el.offsetTop;
    tempEl = el.offsetParent;
    while(tempEl != null){
        yPos += tempEl.offsetTop;
        tempEl = tempEl.offsetParent;
    }
    return yPos;
}

function showHideMenuNode(node_id, image_id){
	n = document.getElementById(node_id);
	i = document.getElementById(image_id);
	if(n){
		n.style.display = n.style.display == "none" ? "block" : "none";
		if(i){
			i.src = n.style.display == "none" ? skin_images + "/menu_tree_plus.gif" : skin_images + "/menu_tree_minus.gif";
		}
	}
}


function getElementHeight(Elem) {
	if(ns4){
		var elem = document.getElementById(Elem);
		return elem.clip.height;
	} else {
		if(document.getElementById) {
			var elem = document.getElementById(Elem);
		} else if (document.all){
			var elem = document.all[Elem];
		}
		if (op5) { 
			xPos = elem.style.pixelHeight;
		} else {
			xPos = elem.offsetHeight;
		}
		return xPos;
	} 
}

function addDash(field, e){
    var unicode=e.keyCode? e.keyCode : e.charCode
    var val = field.value;

    if(unicode!=8){
        if(val.length==3){
            field.value = field.value + "-";
        }else if(val.length==7){
            field.value = field.value + "-";
        }
    }
}

function getElementWidth(Elem) {
	if (ns4) {
		var elem = document.getElementById(Elem);
		return elem.clip.width;
	} else {
		if(document.getElementById) {
			var elem = document.getElementById(Elem);
		} else if (document.all){
			var elem = document.all[Elem];
		}
		if (op5) {
			xPos = elem.style.pixelWidth;
		} else {
			xPos = elem.offsetWidth;
		}
		return xPos;
	}
}


if(document.layers){
	_browser = "nn";
}
if(document.all){
	_browser = "ie";
}
if(navigator.userAgent.toLowerCase().match("gecko")){
	_browser= "gecko";
}



var urlClicked=false;
var win="";

function popupLoadSurvey(){
	
	   for(i=0; i<document.links.length; i++){
        document.links[i].onclick=checklink;  
		//alert(document.links[i]);
	   }

}


function checklink()
{

if(this.toString().match("sampleplan.com|dev.supercart.vstaff.com"))  { 
 document.getElementById("is_survey_popup").value="No";
}else{
   if(this.toString().match("http|https|www"))
       
        popup();
}
 
}
function popup()
{
if(document.getElementById("is_survey_popup").value=="Yes"){
  win=	window.open('http://www.surveymonkey.com/s.aspx?sm=72lojE1Ec2Nz6kJb1Fvn0g%3d%3d', 'win', 'height=650,width=530,scrollbars=yes,resizable=no,status=yes');
  win.focus();
 }else{
	 //Delete_Cookie('Sampleplan_Survey_Popup', '/', '');

 }
}





function SurveyFreeProcess(){
	var fname = document.getElementById("FNAME").value;
	var lname = document.getElementById("LNAME").value;
	var email = document.getElementById("EMAIL").value;
	//var promocode = document.getElementById("PROMOCODE").value;
	var pdf = document.getElementById("select_survey_free").value;
	//alert(pdf);
	if(fname==""){ alert("Please fill-in First Name"); document.getElementById("FNAME").focus();  return false; }
	if(lname==""){ alert("Please fill-in Last Name"); document.getElementById("FNAME").focus();  return false; }
	if(email==""){ alert("Please fill-in Email Address"); document.getElementById("EMAIL").focus();  return false; }
	
	
	if(!isEmail(email)){ alert("Invalid Email Address"); document.getElementById("EMAIL").focus();  return false; }
	if(pdf<1){ alert("Please choose PDF"); document.getElementById("select_survey_free").focus();  return false; }
	//if(promocode==""){ alert("Please enter Promo Code"); document.getElementById("PROMOCODE").focus();  return false; }
	
	document.getElementById("survey_form").submit();
}

// this deletes the cookie when called
function Delete_Cookie( name, path, domain ) {
if ( Get_Cookie( name ) ) document.cookie = name + "=" +
( ( path ) ? ";path=" + path : "") +
( ( domain ) ? ";domain=" + domain : "" ) +
";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

// this fixes an issue with the old method, ambiguous values
// with this test document.cookie.indexOf( name + "=" );
function Get_Cookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );


		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}


function checkContactEmail(){
	document.getElementById("is_survey_popup").value="No";
	
	if(document.getElementById("name").value==""){
		alert("Please fill-in name field.");
		document.getElementById("name").focus();
		return false;
		
	}
	
	if(document.getElementById("mail").value==""){
		alert("Please fill-in email field.");
		document.getElementById("mail").focus();
		return false;
		
	}
	
	if(!isEmail(document.getElementById("mail").value)){
		alert("Invalid email format.");
		document.getElementById("mail").focus();
		return false;
		
	}
	
	if(document.getElementById("mail").value!=document.getElementById("vmail").value){
		alert("Verification email did not match.");
		document.getElementById("mail").focus();
		return false;
		
	}
	
	if(document.getElementById("textarea").value==""){
		alert("Please fill-in your concern in the box below.");
		document.getElementById("textarea").focus();
		return false;
		
	}
	
	return true;
}


function affilliate_subscribe(){
	document.getElementById("is_survey_popup").value="No";
	xajax_processAjaxAction("affilliate_start_subscribe", xajax.getFormValues("aff_form"));
	
}

function MM_openBrWindow(theURL,winName,features)
{window.open(theURL,winName,features);}

function isEmail(entry){
	var rex= /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/	
	return rex.test(entry);
}

function delay(gap){ /* gap is in millisecs */
	var then, now; 
	then = new Date().getTime();
	now = then;
	while((now-then) < gap){
		now=new Date().getTime();
	}
}

function showImage(image_url, image_width, image_height){
	image_width = image_width == "" ? 400 : image_width;
	image_height = image_height == "" ? 380 : image_height;

	var w = screen.width;
	var h = screen.height;
	var ww = image_width * 1 + 30;
	var wh = image_height * 1 + 45;
	var wx = (w - ww)/2;
	var wy = (h - wh)/2;
	
	if(imageWin != null){
		imageWin.close();
	}
	imageWin = null;
	imageWin = window.open(
		"", 
		"ProductImageWindow", 
		"titlebar=yes, toolbar=no, menubar=no, status=no, directories=no, resizable=yes, scrollbars=yes, top=" + wy.toString() + ", left=" + wx.toString() + ", width=" + ww.toString() + ", height=" + wh.toString() + ""
	);
	while(imageWin==null);
	imageWin.focus();
	
	imageWin.document.body.innerHTML = "";
	imageWin.document.write('<body style="padding:5px;margin:0px">');
	imageWin.document.write('<div align="center"><img hspace="0" vspace="0" src="' + image_url + '"></div><br/>');
	imageWin.document.write('<div align="center" style="font-family:arial;font-color:black;font-size:11px;"><a href="javascript:window.close();" style="color:#0000AA;">Close Window</a></div>');
	imageWin.document.write('</body>');
	imageWin.width = ww;
	imageWin.height = wh;
}

function showPrinterPage(url){
	var prWin = null;
	prWin = window.open(
		url,
		"PrintVer", 
		"titlebar=yes, toolbar=no, menubar=yes, status=yes, directories=no, resizable=yes, scrollbars=yes, top=20, left=20, width=810, height=600"
	);
	while(prWin==null);
	prWin.focus();
}

function OnButton(bt){
	document.images[bt].src = skin_images + bt + "_on.gif";
	
}
function OffButton(bt){
	document.images[bt].src = skin_images + bt + "_off.gif";
}

function OnMenu(cid){
	document.images["menul_" + cid].src = skin_images + "catl_bg_on.gif";
	document.all["menur_" + cid].background = skin_images + "catr_bg_on.gif";
}
function OffMenu(cid){
	document.images["menul_" + cid].src = skin_images + "catl_bg_off.gif";
	document.all["menur_" + cid].background = skin_images + "catr_bg_off.gif";
}

function OnMenu(img){
	document.images[img].src = skin_images + "menu_arrow_on.gif";
}
function OffMenu(img){
	document.images[img].src = skin_images + "menu_arrow.gif";
}

function ShowPopup(src){
	var bWin = null;
	bWin = window.open(
		src, 
		"PopupWind", 
		"titlebar=no, toolbar=no, menubar=no, status=no, directories=no, resizable=no, scrollbars=no, top=20, left=20, width=320, height=240"
	);
	while(bWin==null);
	bWin.focus();
}

function PopUpImage(image_source, image_width, image_height){
	var bWin = null;
	bWin = window.open(
		image_source, 
		"ImageWind", 
		"titlebar=no, toolbar=no, menubar=no, status=no, directories=no, resizable=no, scrollbars=no, top=20, left=20, width=" + (image_width + 20) + ", height=" + (image_height + 20)
	);
	while(bWin==null);
	bWin.focus();
}

function ConfirmLogout(){
	document.getElementById("is_survey_popup").value="No";
	if(orderItemsCount > 0){
		
		if(confirm("You have items in your cart. Logging out will empty your cart\nAre you sure want to continue?")){
			document.location = urlLogout;
		}
	}
	else{
		if(confirm("Do you really want to logout?")){
			document.location = urlLogout;
		}
	}
}

function CartConfirmDeleteItem(ocid){
	document.getElementById("is_survey_popup").value="No";
	if(confirm(msg_confirm_delete_item)){
		document.location = CartDeleteItemUrl + '&ocid=' + ocid;
	}
}
function CartConfirmEmpty(){
	document.getElementById("is_survey_popup").value="No";
	if(confirm(msg_confirm_empty_cart)){
		document.location = CartEmptyUrl;
	}
}

