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;
    

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=/"
}
    
    
var global = {
    processing:false,
    processMessage: "Your request is currently being processed, we will appreciate your patience in the matter.",
    location:"../processing/index.aspx",
    fieldid:"",
    loadPath:"../images/ajax-loader.gif",
    loadColor:"#cccccc",
    cartcount: 0,
    
    validation: function(div){
        var $text = $("#" + div + " input:text, #" + div + " input:password, #" + div + " textarea");
        var $select = $("#" + div + " select");
        var $Radio = $("#" + div + " input:radio");
        var $check = $("#" + div + " input:checkbox");
        var $checklist = $("#" + div + " *[checkboxes]");
        var result = true;
        var fields = "";
        this.checkDivId = div; 

        $text.each(function(){        
            if($(this).parents("div").css("display") == "block"){
                $(this).removeClass("inputerror");
    	        //$("#" + $(this).attr('div')).html("");
    	        $('#' + div + ' div[id*="' + $(this).attr("div") + '"]').html("");
    	        if($(this).attr('errorMsg') != null && $(this).attr('errorMsg') != ""){
    	            if($(this).val() == ""){
    	                if(global.fieldid == "") global.fieldid = this.id;
    	                var msg = $(this).attr("errorMsg")
					    if($(this).attr('selecttext') != null && $(this).attr('selecttext') != ""){
					        msg += $("." + $(this).attr('selecttext') + " option:selected").text();
					    }
					    
					    $('#' + div + ' div[id*="' + $(this).attr("div") + '"]').html(msg);
					    //$("#" + $(this).attr("div")).html(msg);
	                    $(this).addClass('inputerror');
	                    result = false;
    	            }
    	            else if($(this).attr('FilterInput') != null){
	                    if(eval($(this).attr('FilterInput') + "('" + this.id + "')") == false){
                            if(global.fieldid == "") global.fieldid = this.id;
				            //$("#" + $(this).attr("div")).html($(this).attr("errorMsg"));
				            $('#' + div + ' div[id*="' + $(this).attr("div") + '"]').html($(this).attr("errorMsg"));
                            $(this).addClass('inputerror');
                            result = false;
                        }
    	            }
    	            else if($(this).attr('compare') != null){
    	                $target = $('#' + div + ' [id*="' + $(this).attr("compare") + '"]');
    	                if($target.val() != $(this).val()){
                            if(global.fieldid == "") global.fieldid = this.id;
		                    //$("#" + $(this).attr("div")).html($(this).attr("errorMsg"));
		                    $('#' + div + ' div[id*="' + $(this).attr("div") + '"]').html($(this).attr("errorMsg"));
                            $(this).addClass('inputerror');
                            result = false;
                        }
    	            }
    	        }
    	        else if(this.getAttribute('refselect') != null && this.getAttribute('refselect') != ""){
    	            if($(this).val() == ""){
    	                if(global.fieldid == "") global.fieldid = this.id;
					    //$("#" + $(this).attr("div")).html("Please enter " + $("." + this.getAttribute('refselect') + " option:selected").text());
					    $('#' + div + ' div[id*="' + $(this).attr("div") + '"]').html("Please enter " + $("option:selected", $('#' + div + ' select[id*="' + $(this).attr("refselect") + '"]')).text());
	                    $(this).addClass('inputerror');
	                    result = false;
    	            }
    	        }
   
                if(typeof $(this).attr("nameid") != "undefined"){
    	            if(fields != "") fields += "&";
			        fields += $(this).attr("nameid") + "=" + $(this).val();
			    }
		    }
        });

        $select.each(function(){
            //$("#" + $(this).attr("div")).html("");
            $('#' + div + ' div[id*="' + $(this).attr("div") + '"]').html("");
            $(this).removeClass("inputerror");
            
            if (this.getAttribute('index') != null && this.getAttribute('index') != ""){
                if(this.options.selectedIndex >= parseInt($(this).attr("index"))){
				    if(fields != "") fields += "&";
				    fields += $(this).attr("nameid") + "=";
                    fields += $(this).val();
			    }
			    else if(this.getAttribute('errorMsg') != null && this.getAttribute('errorMsg') != ""){
    			    if(global.fieldid == "") global.fieldid = this.id;
			        //$("#" + $(this).attr("div")).html($(this).attr("errorMsg"));
			        $('#' + div + ' div[id*="' + $(this).attr("div") + '"]').html($(this).attr("errorMsg"));
                    $(this).addClass('inputerror');
                    result = false;
			    }
            }
            else{
                if(typeof $(this).attr("nameid") != "undefined"){
                    if(fields != "") fields += "&";
			        fields += $(this).attr("nameid") + "=";
                    fields += $(this).val();
                }
            }
		});

		var name = "";
		$Radio.each(function(){
            if(name != $(this).attr('name')){
                name = $(this).attr('name');
               
                $parent = $(this).parent();
                if($parent.parent().css("display") != "none"){
                    $('#' + div + ' div[id*="' + $parent.attr("div") + '"]').html("");
                   
                    if($('input[name=' + name + ']').is(':checked') == false){
                        if(global.fieldid == "") global.fieldid = $parent.attr("id");
			            //$("#" + $parent.attr("div")).html($parent.attr("errorMsg"));
			            $('#' + div + ' div[id*="' + $parent.attr("div") + '"]').html($parent.attr("errorMsg"));
			            result = false;
                    }
                    else {
                        if(typeof $parent.attr("nameid") != "undefined"){
                            if(fields != "") fields += "&";
				            fields += $parent.attr("nameid") + "=";
                            fields += $('input[name=' + name + ']:checked').val();
                        }
                    }
                }
            }
        });
  
       /*$check.each(function(){
            if($(this).is(':checked') == true){
                if(fields != "") fields += "&";
                if(typeof $(this).parent().attr("nameid") != "undefined")
		            fields += $(this).parent().attr("nameid") + "=1";
            }
        });
        */

        $checklist.each(function(){
            var value = "";
            if($checklist.parent().css("display") != "none"){
                $('#' + div + ' div[id*="' + $(this).attr("div") + '"]').html("");

                $("input:checkbox", $(this)).each(function(){
                    if($(this).is(':checked') == true){
                        if(value != "") value += "&";
                        if(typeof $(this).parent().attr("nameid") != "undefined")
		                    value += $(this).parent().attr("nameid") + "=1";
                    }
                });
                
                if(value == ""){
                    if($(this).attr('errorMsg') != null && $(this).attr('errorMsg') != ""){
                        $('#' + div + ' div[id*="' + $(this).attr("div") + '"]').html($(this).attr("errorMsg"));
                        result = false;
                    }
                }
                if(fields != "") fields += "&";
                fields += value;
            }
        });
 
        this.fields = fields;
        return result;
    },
    
    ErrorMessage: function(){
        var div = this.error.split("|");
	    for(i = 0; i < div.length; i++){
	        $obj =  $('*[nameid*="' + div[i] + '"]');
	        $('div[id*="' + $obj.attr("div") + '"]').html($obj.attr("errorMsg"));
	        $obj.addClass('inputerror');
	    }
    },
    
    GetHTMLData: function(response){
        this.error = "";
        this.message = "";
        this.warning = "";
        this.shopingBagStr = "";
        this.checkoutBagStr = "";
        this.cartcount = "";
        this.freecount = "";
        this.RewardPrintCount = "";
        this.rewardon = "false";
        this.addon = "";
        this.html = "";
        this.Url = "";

        var div = document.createElement("div");
        div.innerHTML = response;
        var elements = div.childNodes;
        
        for(var i = 0; i < elements.length; i++){
            switch(elements[i].id){
                case "Error":
                    this.error = elements[i].innerHTML;
                    break;
                case "Warning":
                    this.warning = elements[i].innerHTML;
                    break;
                case "Message":
                    this.message = elements[i].innerHTML;
                    break;
                case "ShoppingBag":
                    this.shopingBagStr = elements[i].innerHTML;
                    break;
                case "CkeckoutBag":
                    this.checkoutBagStr = elements[i].innerHTML;
                    break;
                case "CartCount":
                    this.cartcount = elements[i].innerHTML;
                    break;
                case "FreeCount":
                    this.freecount = elements[i].innerHTML;
                    break;
                case "IsRewardOn":
                    this.rewardon = elements[i].innerHTML;
                    break;
                case "IsAddOn":
                    this.addon = elements[i].innerHTML;
                    break;
                case "Html":
                    this.html = elements[i].innerHTML;
                    break;
                case "URL":
                    this.Url = elements[i].innerHTML;
                    break;
            }
        }
    },
    
    GetXMLData:function(doc){
        var errorNode = doc.getElementsByTagName("Error");
        var messageNode = doc.getElementsByTagName("Message");
        var statusNode = doc.getElementsByTagName("BookingStatus");
        var warningNode = doc.getElementsByTagName("Warning");
        var redirectNode = doc.getElementsByTagName("URL");
        this.error = "";
        this.message = "";
        this.warning = "";
        this.Url = "";
        this.status = "";
        
	    if (errorNode.length > 0) {
			if (errorNode[0].firstChild != null) {
				this.error = errorNode[0].firstChild.nodeValue;
			}
		}
		if (messageNode.length > 0) {
			if (messageNode[0].firstChild != null) {
				this.message = messageNode[0].firstChild.nodeValue;
			}
		}
		if (statusNode.length > 0) {
			if (statusNode[0].firstChild != null) {
				this.status = statusNode[0].firstChild.nodeValue;
			}
		}
		if (warningNode.length > 0) {
			if (warningNode[0].firstChild != null) {
				this.warning = warningNode[0].firstChild.nodeValue;
			}
		}
		if (redirectNode.length > 0) {
			if (redirectNode[0].firstChild != null) {
				this.Url = redirectNode[0].firstChild.nodeValue;
			}
		}
    },
    
    MasterHeight: function(){
        $("#OuterFrame").height($(".vPackageOuter").height() + $(".footerlink").height() + 20);
    },
    
    processingLoad: function(tblName){
        this.loading = new loader();
        this.loading.path = this.loadPath;
        this.loading.color = this.loadColor;
        this.loading.tblName = tblName;
        this.loading.loading();
    },
    pageHeight:function(){
        this.outFrame.height(this.inFrame.height() + this.bottomFrame.height());
    },
    countdownLoad: function(id){
        this.loading = new countdownloader();
        this.loading.tblName = id;
        this.loading.color = this.loadColor;
        this.loading.loading();
    }
}
function countdownloader(){
	this.div = document.createElement("div");
	this.color = "";
    this.tblName = "";
}

countdownloader.prototype.loading = function(){
    $obj = $("#" + this.tblName);
    
    width = $obj.width();
    height = $obj.height();
    this.div.style.backgroundColor = this.color;
    this.div.align = "center"
	$(this.div).addClass("divCoutDown");
	document.body.appendChild(this.div);
}

countdownloader.prototype.removeLoading = function(){
	$(this.div).remove();
}

function loader(){
    this.frame = document.createElement("iframe");
	this.div = document.createElement("div");
	this.path = "";
    this.color = "";
    this.tblName = "";
}

loader.prototype.loading = function(){
    var $obj = $("#" + this.tblName);
    
    width = $obj.width();
    height = $obj.height();
    style = this.frame.style;
    style.opacity = .5;
	style.MozOpacity = .5;
	style.KhtmlOpacity = .5;
	style.filter = "alpha(opacity=50)";
	style.height = height + "px";
	style.width = width + "px";
	style.position = "absolute";
	style.backgroundColor = this.color;
	style.zIndex="9999";
	style.top = $obj.position().top + "px";
	style.left = $obj.position().left + "px";
	document.body.appendChild(this.frame);
	var image = document.createElement("img");
	image.src = this.path;
	this.div.appendChild(image);
	this.div.style.position = "absolute";
	$(this.div).css("zIndex","9999");
	this.div.style.top = $obj.position().top + ((parseInt(height)/2) -50)  + "px";
	this.div.style.left= $obj.position().left + ((parseInt(width)/2) - 50) + "px";
	document.body.appendChild(this.div);
}

loader.prototype.removeLoading = function(){
    $(this.frame).remove();
	$(this.div).remove();
}

