// JavaScript Document
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 PopupCenter(pageURL, title,w,h) {
		var left = (screen.width/2)-(w/2);
		var top = (screen.height/2)-(h/2);
		var set = "toolbar=no, location=no, status=no, menubar=no, scrollbars=yes, resizable=no, copyhistory=no, width="+w+", height="+h+", top="+top+", left="+left+", directories=no";
		var targetWin = window.open(pageURL,title,set);
} 
	

function popImage(imageURL, caption){
  var windowTop = 100; var windowLeft = 100; var defaultWidth = 730;
  var defaultHeight = 532; var onLoseFocusExit = true; var undefined;
  var Options = "width=" + defaultWidth + ",height=" + defaultHeight + ",top=" + windowTop + ",left=" + windowLeft + ",resizable"  
  var myScript = "<html>\n" + 
    "<head>\n" + 
    "<title>" + caption + "\</title>\n" +
    "<script language=\"JavaScript\" type=\"text/javascript\">\n" +
    "function resizewindow () {\n" +
    "  var width = document.myimage.width;\n" + 
    "  var height = document.myimage.height;\n";
  if (navigator.appName.indexOf("Netscape") != -1) {  myScript = myScript +  "  window.innerHeight = height;\n  window.innerWidth = width;\n"
  }else if (navigator.appName.indexOf("Opera") != -1) {   myScript = myScript +  "  window.resizeTo (width+12, height+71);\n"
  }else if (navigator.appName.indexOf("Microsoft") != -1) { myScript = myScript + "  window.resizeTo (width+12, height+71);\n" 
  }else {   myScript = myScript + "  window.resizeTo (width+14, height+34);\n"  }
  myScript = myScript + "}\n" + "window.onload = resizewindow;\n" +
    "</script>\n</head>\n" + "<body ";
  if (onLoseFocusExit) {myScript = myScript + "onblur=\"self.close()\" ";}
  myScript = myScript + "style=\"margin: 0px; padding: 0px;\">\n" +
    "<img src=\"" + imageURL + "\" alt=\"" + caption + "\" title=\"" + caption + "\" name=\"myimage\">\n" + 
    "</body>\n" +  "</html>\n";  
  var imageWindow = window.open ("","imageWin",Options);
  imageWindow.document.write (myScript)
  imageWindow.document.close ();
  if (window.focus) imageWindow.focus();
  return false;
}

function setCookie(sName, sValue){
  date = new Date();
  document.cookie = sName + "=" + escape(sValue) + "; expires=Fri, 31 Dec 2099 23:59:59 GMT;";
  //GetCookie(sName,"Cookie Set: " + sName + "=" + sValue); //For debug output purposes.
}
 
function getcookietime(sName, msg){
  var aCookie = document.cookie.split("; ");
  var out = "Cookie string: " + aCookie; //For debug output purposes.
  for (var i=0; i < aCookie.length; i++){
    var aCrumb = aCookie[i].split("=");
    if (sName == aCrumb[0]) {
		return unescape(aCrumb[1]);
    }
  }
  return null;
}
 
function DelCookie(sName){
  document.cookie = sName + "=; expires=Fri, 21 Dec 1976 04:31:24 GMT;";
}

function setcookietime(sName,sValue,duration){
  date = new Date();
  document.cookie = sName + "=" + escape(sValue) + "; expires=Fri, 31 Dec 2099 23:59:59 GMT;";
}

function doPostSingle(){
	var totalqty = jQuery("#amt_qty").attr("scope").toString();
	var pre_qty =jQuery("#amt_qty").val();
	if(parseFloat(totalqty) >= parseFloat(pre_qty)){
		document.frm_product.submit();
	} else {
		jQuery("#amt_qty").val(totalqty);
		alert("The amount of product(s) exceeds inventory, and has been corrected.");	
	}
}

function FieldValided(sFrmName,sFieldName){
	return jQuery('#'+sFrmName).validate().element('#'+sFieldName);
}

//Valided the Form
function ValidForm(frmName){
	//Search for all Input Area with isreq=1
		jQuery.validator.addMethod("field", function(value, element) {
			return value != 0;
		}, "&nbsp;");
		jQuery("#form1").validate({
			errorPlacement: function(error, element) { 
				error.appendTo(element.next() ); 
			},			
			invalidHandler: function(e, validator) {
				var errors = validator.numberOfInvalids();
				if (errors) {
					var message = errors == 1
						? 'You missed 1 field. It has been highlighted below'
						: 'You missed ' + errors + ' fields.  They have been highlighted below';					
						jQuery("div.error span.msg").html(message);
					jQuery("div.error").show();
				} else {
					jQuery("div.error").hide();
				}
			},
			onkeyup: false,
			success: function(label) { 
				label.html("&nbsp;").toggleClass("valid"); 
	        },debug:true
		});
	if(jQuery("#form1").valid()){
		document.getElementById("form1").submit();
	}
}

function OtherAMTValid(NameID,target) {
	if(isNaN(document.getElementById(NameID).value)){
		alert("Please enter your Contribution Amount in Correct Format");
		document.getElementById(NameID).focus();
	}
	if(parseFloat(document.getElementById(NameID).value)<target || document.getElementById(NameID).value==''){
		alert("Please enter your Contribution Amount more than or equal to $"+target);
		jQuery('#amount').val(jQuery('#r5').val());
		jQuery('#otheramt').val(jQuery('#r5').val());
	}
}

function doPostDonation(frmName){
	if(jQuery("#frmDonation").valid()){
		jQuery("div.error").hide();
		//jQuery("#frmDonation").submit();
		document.getElementById("frmDonation").submit();
	}
}

function showlink(sURL){
	var gaJsHost = (("https:" == document.location.protocol) ? "https://" : "http://"); 
	if(sURL.substr(0,3)=='www'){
		sURL = gaJsHost+sURL;
	}
	window.location.href = sURL;
}

function cancelrole(p_txt,textbox,btn1,btn2){
	if(jQuery("#"+btn1).html()=="Cancel"){
		jQuery("#"+p_txt).html("Cancelled");
		jQuery("#"+btn1).html("Reset");
		jQuery("#"+textbox).val(8);
	} else {
		jQuery("#"+p_txt).html("Pending");
		jQuery("#"+btn1).html("Cancel");
		jQuery("#"+textbox).val(2);
	}
}
function active_role(p_txt,textbox,btn3){
	if(jQuery("#"+btn3).html()=="Add"){
		jQuery("#"+p_txt).html(" - Active");
   		jQuery("#"+btn3).html("Reset");
    	jQuery("#"+textbox).val(5);
   	} else {
		jQuery("#"+p_txt).html("");
    	jQuery("#"+btn3).html("Add");
   		jQuery("#"+textbox).val(0);
    }
}
function InActive_role(p_txt,textbox,btn3){
	if(jQuery("#"+btn3).html()=="Remove"){
		jQuery("#"+p_txt).html(" - InActive");
   		jQuery("#"+btn3).html("Reset");
    	jQuery("#"+textbox).val(9);
   	} else {
		jQuery("#"+p_txt).html("");
    	jQuery("#"+btn3).html("Remove");
   		jQuery("#"+textbox).val(1);
    }
}

function ApplyRole(userids,selRole,paths){
	jQuery.ajax({
		url : paths+"applyrole.php",
		cache: false,
		data: "tracker=roleupdate&role="+selRole+"&userids="+userids,
		success : function (data) {
			if(data == ''){
				if(selRole=='GV' || selRole=='RO'){
					document.location.href = 'index.php?mode=fe&page=profile';
				} else if(selRole=='BM' || selRole=='CP' || selRole=='SM' || selRole=='AP') {
					document.location.href = 'index.php?mode=fe&page=home';
				}
			} else {
				alert('Error : '+data);
			}
		}
	});
}
function visithome(){
	document.location.href = 'index.php';
}

jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};