var ns6=document.getElementById&&!document.all;
var ie=document.all;
var ContainerID = "";
var processing = false;
var emailfilter=/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i;
var PanelArray = new Array("ShopStepPortrait","ShopStepPack","ShopStepDesign","ShopStepPackagingChoice");
var windowHeight;

if( typeof( window.innerWidth ) == 'number' ) windowHeight = window.innerHeight;
else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) //IE 6+ in 'standards compliant mode'
    windowHeight = document.documentElement.clientHeight;
else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) )//IE 4 compatible
    windowHeight = document.body.clientHeight;
    
var getOffsetHeight = 
    function(obj) {
	    return obj.offsetHeight;
};

function EmailValid(ID){
    var emailfilter=/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i;
    var returnval = emailfilter.test($("#" + ID).val());
    return returnval;
}

function FilterInput(Value){ 
    var anum=/(^\d+$)|(^\d+\.\d+$)/
    var testresult = false;
    
    if (anum.test(Value)) testresult = true;
    
    return testresult;
}
function CardNumberCheck(ID){
    $CardNo = $("#" + ID);
    
    $CardNo.removeClass("inputerror");
    $('div[id*="' + $CardNo.attr("div") + '"]').html("");
    var msg = $CardNo.attr("errorMsg")
    	        
    if(FilterInput($CardNo.val()) == false){	
		$('div[id*="' + $CardNo.attr("div") + '"]').html(msg);
		$CardNo.addClass('inputerror');
    }
    else{
        if($CardNo.val().length < 16){
            $('div[id*="' + $CardNo.attr("div") + '"]').html(msg);
		    $CardNo.addClass('inputerror');
        }
    }
}

function SecurityCodeCheck(ID){
    $CardNo = $("#" + ID);
    $('div[id*="' + $CardNo.attr("div") + '"]').html("");
    var msg = $CardNo.attr("errorMsg")
    
    if($('select[id*="ddlCartType"]').val() == "14"){
        if($CardNo.val().length < 4 || $CardNo.val().length > 6){
            $('div[id*="' + $CardNo.attr("div") + '"]').html(msg);
		    $CardNo.addClass('inputerror');
        }
    }
    else{
        if(FilterInput($CardNo.val()) == false){
            $('div[id*="' + $CardNo.attr("div") + '"]').html(msg);
		    $CardNo.addClass('inputerror');;
        }
        else{
            if($CardNo.val().length != 3){
                $('div[id*="' + $CardNo.attr("div") + '"]').html(msg);
		        $CardNo.addClass('inputerror');
            }
        }
    }
}
    
function KeyPressFilter(filterType, evt, format, obj){
    var keyCode, Char, inputField, filter = ""; 
    var alpha = "ampAMP";
    var num   = "0123456789"; 
    var phone="0123456789-+ ";

    // Get the Key Code of the Key pressed if possible else - allow 
    if(window.event){ 
        keyCode = window.event.keyCode; 
        evt = window.event; 
    }else if (evt)keyCode = evt.which; 
    else return true; 
    
    // Setup the allowed Character Set 
    if(filterType == 0) filter = alpha; 
    else if(filterType == 1) filter = num; 
    else if(filterType == 2) filter = phone; 
   
    if(filter == '')return true; 

    // Get the Element that triggered the Event 
    inputField = evt.srcElement ? evt.srcElement : evt.target || evt.currentTarget; 
    // If the Key Pressed is a CTRL key like Esc, Enter etc - allow 

    if((keyCode==null) || (keyCode==0) || (keyCode==8) || (keyCode==9) || (keyCode==13) || (keyCode==27) )return true; 
    // Get the Pressed Character 
    Char = String.fromCharCode(keyCode); 
    // If the Character is a number - allow 

    if((filter.indexOf(Char) > -1)){
	    var x=obj.value
	    if (format=='h'){
		    if(SelectValue!="" && x!=""){
			    if(SelectValue.length==x.length) {
				    SelectValue=""
				    return true;
			    }
			    else {
				    if(StartPoint==0){
					    if(parseInt(x.substr(1,1)) >= 0 && parseInt(x.substr(1,1)) <= 2) {
						    if(parseInt(Char) <= 1)return true
						    else return false
					    }
					    else return false
				    }
				    else {
					    if(x.substr(0,1)=="0")return true
					    else{
						    if(parseInt(Char) <= 2)return true;
						    else return false;
					    }
				    }
			    }
		    } 
		    else{
			    if(x.length>0){
				    if (x=="0")return true;
				    else if (x=="1"){
					    if(parseInt(Char) <= 2)return true;
					    else return false;
				    }
				    else return false;
			    }
			    else return true
		    }
	    }
	    else if (format=='m'){
		    if(SelectValue!="" && x!=""){
			    if(SelectValue.length==x.length) {
				    SelectValue=""
				    return true;
			    }
			    else {
				    if(StartPoint==0){
					    if(parseInt(Char) <= 5)return true
					    else return false

				    }
				    else {
					    if(parseInt(x.substr(0,1))<=5)return true;
					    else return false;
				    }
			    }
		    } 
		    else{
			    if(x.length>0){
				    if (parseInt(x)<=5)return true;
				    else return false;
			    }
			    else return true
		    }
	    }
	    else if(format=="ap"){
		    if(SelectValue!="" && x!=""){
			    if(SelectValue.length==x.length) {
				    if(Char=="M" || Char=="m") return false;
				    else {
					    if(Char=="a" || Char=="A") obj.value="AM";
					    else obj.value="PM";
				    }	
			    }
			    else {
				    if(StartPoint==0){
					    if(Char=="M" || Char=="m") return false;
					    else {
						    if(Char=="a" || Char=="A") obj.value="AM";
						    else obj.value="PM";
					    }	
				    }
			    }
		    } 
		    else{
			    if(Char=="M" || Char=="m") return false;
			    else {
				    if(Char=="a" || Char=="A") obj.value="AM";
				    else obj.value="PM";
			    }	
		    }
		    obj.focus()
		    obj.select()
		    return false;
	    }
	    else{
	        $("." + $(obj).attr("label")).html("");
	        return true; 
	    }
    }
    else{
        $("." + $(obj).attr("label")).html("Invalid input");
        return false; 
    }
}

function getCookie(Name){ 
    var re=new RegExp(Name+"=[^;]*", "i"); //construct RE to search for target name/value pair
    if (document.cookie.match(re)) //if cookie found
	    return document.cookie.match(re)[0].split("=")[1] //return its value
    return null
}
    
function setCookie(name, value, days){
    if (typeof days!="undefined"){ //if set persistent cookie
	    var expireDate = new Date()
	    expireDate.setDate(expireDate.getDate()+days)
	    document.cookie = name+"="+value+"; path=/; expires="+expireDate.toGMTString()
    }
    else //else if this is a session only cookie
	    document.cookie = name+"="+value+"; path=/"
}
    
function $get(id){
	if(ie)return document.all[id];
	else if(ns6) return document.getElementById(id);
}

function OnMouseOver(obj,srcurl){
	if(ie) obj.style.cursor="hand";
	else if(ns6) obj.style.cursor="pointer";

	if(srcurl != null)obj.src = srcurl;
}

function OnMouseOut(obj,srcurl){
	obj.src = srcurl;
}

function trim(str){
	s = str.replace(/^(\s)*/, '');
	s = s.replace(/(\s)*$/, '');
	return s;
}

function GetXmlHttpObject(){ 
	var objXMLHttp=null;
	if (window.XMLHttpRequest){
		objXMLHttp=new XMLHttpRequest();
	}
	else if (window.ActiveXObject){
		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	return objXMLHttp;
} 

function GetProcessBarDisplay(div){
	var myWidth = $get("DIVMain").offsetWidth,/*screen.width,*/ myHeight = $get("DIVMain").offsetHeight ;
	var oDiv = document.createElement("iframe");
	var oStyle = oDiv.style;
	var DivRef = $get(div);

	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} 
	else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} 
	else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}

	oDiv.id="transDiv";
	oStyle.opacity = .5;
	oStyle.MozOpacity = .5;
	oStyle.KhtmlOpacity = .5;
	oStyle.filter = "alpha(opacity=50)";      
	oStyle.top = "0px";
	oStyle.left = "0px";
	oStyle.height = $get("DIVMain").offsetHeight + "px";
	oStyle.width = "100%";
	oStyle.position = "absolute";
	oStyle.backgroundColor = "#cccccc";
	oStyle.zIndex="4";
	document.body.appendChild(oDiv);

	//disableAll(document.body);
	//DivRef.style.display="block";
	DivRef.style.top = ((parseInt(myHeight) / 2) - 50) + "px";
	DivRef.style.left= ((parseInt(myWidth) / 2) - 50 ) + "px";
    DivRef.style.display = "block";
	//if (DivRef.style.display=="none") 
	//else DivRef.style.display = "none";
}

function GetProcessBarRemove(){
    try{
	    $get("ProcessBar").style.display="none";
	    var oDiv = $get("transDiv");
        if(oDiv != "undefined" || oDiv != null)document.body.removeChild(oDiv);
    }
    catch(e){}
}

function ProcessingBar($obj){
    var myWidth = $get("DIVMain").offsetWidth,/*screen.width,*/ myHeight = $get("DIVMain").offsetHeight ;
	var oDiv = document.createElement("iframe");
	var oStyle = oDiv.style;

	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} 
	else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} 
	else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}

	oDiv.id="transDiv";
	oStyle.opacity = .5;
	oStyle.MozOpacity = .5;
	oStyle.KhtmlOpacity = .5;
	oStyle.filter = "alpha(opacity=50)";      
	oStyle.top = "0px";
	oStyle.left = "0px";
	oStyle.height = $get("DIVMain").offsetHeight + "px";
	oStyle.width = "100%";
	oStyle.position = "absolute";
	oStyle.backgroundColor = "#cccccc";
	oStyle.zIndex="4";
	document.body.appendChild(oDiv);

	$("#ProcessBar").offset({ top: $obj.offset().top, left: $obj.offset().left });
    $("#ProcessBar").css("display","block");
}

function checkboxTick(obj){
	checkbox = $get(obj);
		
	if(checkbox.checked==false)checkbox.checked=true;
	else checkbox.checked=false;
}

function FindParentContainerID(objID){
    ContainerID = objID.substring(0,objID.lastIndexOf("_") + 1);
}

function setCursorAtEnd(objID) {
	var oTextbox = $get(objID);

	if(typeof oTextbox !="undefined" && typeof oTextbox != null){
		if (oTextbox.createTextRange) {
			var r = (oTextbox.createTextRange());
			r.moveStart('character', (oTextbox.value.length));        
			r.collapse();
			r.select();
		}
		else{
			oTextbox.value=oTextbox.value;
			oTextbox.focus();
		}
	}
}

function PopUpWindow(sUrl,width,height,target,position,toolbar,location){
	var LeftPosition = (screen.width)?Math.round((screen.width-width)/2):100;
	var TopPosition = (screen.height)?Math.round((screen.height-height)/2):100;
	var setting;
	if(position!="center")TopPosition = "500px";

	setting = "width=" + width + "px,height=" + height + "px,top=" + TopPosition + "px,left=";
	setting += LeftPosition + "px,scrollbars=yes,toolbar=" + toolbar + ",location=" + location 
	setting += ",directories=no,status=no,menubar=no,resizable=no,modal=yes'";

	window.open(sUrl,target,setting)
}

function checkmail(e){
	var returnval = emailfilter.test(e);
	return returnval;
}

function OnMouseOverHighlight(obj){
	obj.style.backgroundColor="#d0bbba";
	obj.style.textDecoration = "underline";
}

function OnMouseOutIdle(obj){
	obj.style.backgroundColor="#ffffff";
	obj.style.textDecoration = "none";
}

var general = {
    GetProcessBarDisplay : function(div){
	    var myWidth = $(".mFrame").width(),myHeight = $(".mFrame").height() ;
	    var oDiv = document.createElement("iframe");
	    var oStyle = oDiv.style;
	    var DivRef = $get(div);

	    if( typeof( window.innerWidth ) == 'number' ) {
		    //Non-IE
		    myWidth = window.innerWidth;
		    myHeight = window.innerHeight;
	    } 
	    else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		    //IE 6+ in 'standards compliant mode'
		    myWidth = document.documentElement.clientWidth;
		    myHeight = document.documentElement.clientHeight;
	    } 
	    else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		    //IE 4 compatible
		    myWidth = document.body.clientWidth;
		    myHeight = document.body.clientHeight;
	    }

	    oDiv.id="transDiv";
	    oStyle.opacity = .5;
	    oStyle.MozOpacity = .5;
	    oStyle.KhtmlOpacity = .5;
	    oStyle.filter = "alpha(opacity=50)";      
	    oStyle.top = "0px";
	    oStyle.left = "0px";
	    oStyle.height = $get("DIVMain").offsetHeight + "px";
	    oStyle.width = "100%";
	    oStyle.position = "absolute";
	    oStyle.backgroundColor = "#cccccc";
	    oStyle.zIndex="4";
	    document.body.appendChild(oDiv);

	    //disableAll(document.body);
	    //DivRef.style.display="block";
	    DivRef.style.top = ((parseInt(myHeight) / 2) - 50) + "px";
	    DivRef.style.left= ((parseInt(myWidth) / 2) - 50 ) + "px";
        DivRef.style.display = "block";
	    //if (DivRef.style.display=="none") 
	    //else DivRef.style.display = "none"; 
    }
}
