/* aescripts.js
 * author: Sree
 * last modified on: 6/1/2009
 This file will contain all javascripts used by Artfully Elegant.
 */

function moveToCart(prodid,selSize,wishid)
{
   document.location.href="/cartprocess.php?user_action=m&prod_id="+prodid+"&size="+selSize+"&wish_id="+wishid;
}

function shipmethodChanged(obj,fsh){
    if(obj.value < 3 && fsh ==1)
        alert("Note : Free Shipping Coupons Apply Only For Standard Shipping");
}

function addToCart(prodid)
{
  //This function will send the size as valueid_valuetext
   if (document.getElementById("sel_prodsize") != null) {
        selSize=document.getElementById("sel_prodsize").value;
        if(selSize == "74_Custom Size") {
            //Fetch value in size
            theSize=document.getElementById("txtSize").value;
            //Validate value
            if(theSize==""){
                alert("You have selected Custom Size. Please provide the size");
            }else if(isNaN(theSize)){
                alert("Please enter a valid value for size");
            }else{
                //Prefix valueid
               selSize="74_"+theSize;
               document.location.href="/cartprocess.php?user_action=a&prod_id="+prodid+"&size="+selSize;
            }   
        }else{
            //Not a custom size
            if (selSize=="") {
                //Size not changed in product page
                size=document.getElementById('cboSize').value;
                selSize=size;
            }
          document.location.href="/cartprocess.php?user_action=a&prod_id="+prodid+"&size="+selSize;
        }
    }else{
        //The page does not have a size attribute
      document.location.href="/cartprocess.php?user_action=a&prod_id="+prodid;
    }
}
function addToWL(prodid)
{
  // alert(prodid);
  //This function will send the size as valueid_valuetext
   if (document.getElementById("sel_prodsize") != null) {
        selSize=document.getElementById("sel_prodsize").value;
        if(selSize == "74_Custom Size") {
            //Fetch value in size
            theSize=document.getElementById("txtSize").value;
            //Validate value
            if(theSize==""){
                alert("You have selected Custom Size. Please provide the size");
            }else if(isNaN(theSize)){
                alert("Please enter a valid value for size");
            }else{
                //Prefix valueid
               selSize="74_"+theSize;
               document.location.href="/cartprocess.php?user_action=w&prod_id="+prodid+"&size="+selSize;
            }
        }else{
            //Not a custom size
            if (selSize=="") {
                //Size not changed in product page
                size=document.getElementById('cboSize').value;
                selSize=size;
            }
          document.location.href="/cartprocess.php?user_action=w&prod_id="+prodid+"&size="+selSize;
        }
    }else{
        //The page does not have a size attribute
      document.location.href="/cartprocess.php?user_action=w&prod_id="+prodid;
    }
}

function gotoCart()
{
    document.location.href="/cart.php";
}

function sizeChange(obj,size){
    thesize=document.getElementById(size);
    if(obj.value == "74_Custom Size"){
        if (thesize != null) thesize.style.display="block";
    } else {
        if (thesize != null) {
            thesize.value="";
            thesize.style.display="none";
        }
    }
    if (document.getElementById('sel_prodsize') != null) {
        psize=document.getElementById('sel_prodsize');
        psize.value=obj.value;
    }
  /*   if (document.getElementById('txtSize') != null) {
         size=document.getElementById('txtSize');
         size.value=obj.value;
     }*/
}
function couponkeypress(obj,e)
{
  if(e.keyCode == 13)
  {
     if(obj.value != "")
     {
       btn=document.getElementById("applycoupon");
       if(btn != null) btn.click();

       if (window.event) {
           event.returnValue = false;
           event.cancelBubble = true;
       }
        //For Firefox
       if (e.stopPropagation()) {
           e.stopPropagation();
           e.preventDefault();
           return false; // IE style
       }
     }
  }
}

 function showCart(divID,prod_id)
 {
   	// this function has a dependency on ajaxdelegate.js'
   	//alert("In showcart");
   	npid=document.getElementById("new_prodid");
   // alert(npid.value);
   	if(npid.value != "")
   	{
   		if(parseInt(npid.value) != parseInt(prod_id))
   	   	prod_id=npid.value;
   	}   	
    //alert(prod_id);
   	var requestUrl = '/showcartbutton.php?prod_id=' + prod_id ;
    var ajax = new AjaxDelegate(requestUrl, showCartButton, divID, prod_id);
    ajax.Fetch();
  }

function showCartButton(url, response,divID, prod_code) {
	// the callback method
    //alert('in showCartButton');
	obj=document.getElementById(divID);
	if(obj != null){
		obj.innerHTML = response;
	}
} 

function goshop() {
	location.href="showartists.php";
}	

function validateCoupon() {
   cid=document.getElementById('couponid').value;
   document.getElementById('couponcode').value=cid;
   document.location.href="cartprocess.php?user_action=v&cid="+cid;
}

  function returnToPrev(gotourl)
  {
    document.location.href=gotourl;
  }
function showEmailSignup(){
    eid="";
    obj=document.getElementById('emailID');
    if(obj.value != "Email Sign-up")
        eid=obj.value;
    url=location.pathname;
    //To avoid storing it if you are on the signup page
    if (url != "/emailsignup.php")
    {
        if(eid !="")
            document.location.href= "/emailsignup.php?f="+url+"&e="+eid;
        else
            document.location.href= "/emailsignup.php?f="+url;
     }
}

  function onEmailSignup(e) {
        //the purpose of this function is to allow the enter key to
        //point to the correct button to click.
        var key;

         if(window.event)
              key = window.event.keyCode;     //IE
         else
              key = e.which;     //firefox

        if (key == 13)
        {
            //alert("In email signup");
            //Get the button the user wants to have clicked
            var btn = document.getElementById('btnSignup');
            if (btn != null)
            { //If we find the button click it
                btn.click();
                event.keyCode = 0
            }
        }
   }



/*
function checkout() {
		document.getElementById('user_action').value = 'c';
	  document.form[0].submit();



	 cid=document.getElementById('couponid').value;
   document.location.href="cartprocess.php?user_action=c";
}
*/
/*
function makepayment() {
	document.getElementById('user_action').value = 'p';
	document.form[0].submit();
}

function showNewValues(obj)
{
 //Price
  var nprice=document.getElementById("price");
  var strp= "<B> Sale Price : </B> $"+obj.value;
  nprice.innerHTML=strp;
  //Title
  var ntitle=document.getElementById("ptitle");
  var indx = obj.selectedIndex;
  var selected_text = obj.options[indx].text;
	var strt= "<B> Title : </B>"+selected_text;
	ntitle.innerHTML=strt;
	//Change new_prodid
	pid=document.getElementById("prod_id");
  npid=document.getElementById("new_prodid");
  npid.value=parseInt(pid.value) + parseInt(indx);
	alert(npid.value);
 }
*/


/*
function checkAll(obj) {
	var flg = false ;
  if (obj.checked) { flg = true; }
  obj1 = document.getElementsByName(obj.name);
	for (i=1;i<obj1.length; i++)
	{
	  obj1[i].checked = flg;
	}
}

function getCheckedValue(name) {
  var st=document.location.href;
  var chkdIDs = '0' ;
  obj1 = document.getElementsByName(name);
	for (i=0;i<obj1.length; i++)
	{
		if(obj1[i].checked){
			chkdIDs = chkdIDs + ',' + obj1[i].value;
		}
	}
	if ( chkdIDs == '0'||chkdIDs == ''){
	   alert ("No records selected");
	   return false;
	 }else{
 	 var ans= confirm("Selected records will be deleted.Do you wish to continue?");
   if (ans==true)
    {
     // R>> Add code here to remove the '0 from the string
      var redirectTo=document.location.href;
     if (st.indexOf("prod_list.php") != -1){
       redirectTo+="prod_process.php?prodIds="+chkdIDs;
       window.location=redirectTo;
      }
    }
	} 
}
*/
/*Code from :  http://wcs.wayne.edu/blog/2008/05/28/javascript-on-window-close-event/ - Using jquery

$(window).bind('load', function(e){
  close_window = true;
  logged_out = false;
  $("a").bind('click', function(e){
    if(!$(this).hasClass('clickable'))
      close_window = false;
    });
  $("form").bind('submit', function(e){
    close_window = false;
  });
  if (window.addEventListener) {
    window.addEventListener('beforeunload', message, false);
  } else {
    window.attachEvent('onbeforeunload', message);
  }
});

function message(ev) {
  if(close_window){
    if (ev.stopPropagation) {
      ev.stopPropagation();
    } else {
      ev.cancelBubble = true;
    }
    if (ev.preventDefault) {
      logout();
    } else {
      logout();
      ev.returnValue = "Leaving?";
    }
  }
}

function logout(){
  if(!logged_out){
    $.ajax({
      type: "GET",
      url: '../admin/logout.php',
      data: "logout=true",
      async: false,
      cache: false,
      success: function(msg){
        console.log("Ajax Repsonse: "+msg);
        logged_out = true;
      }
    });
  }
}
*/


function notYetImplemented() {
    alert('Thank you for your interest. This feature has not yet been implemented, please try again later.');
    return false;
}


function MaxProdChange(obj) {
    if (obj != null) {
        document.forms[0].submit();
    }
}

function clearval(){
    searchtb = document.getElementById('searchInput');
    srchStr=searchtb.value;
    if(srchStr == "Enter a Keyword")
      {
       searchtb.value="";
       searchtb.focus();
      }
}

function search() {
    keyword = '';
    searchtb = document.getElementById('searchInput');
    if (searchtb != null) {
        keyword = searchtb.value;
    }
    // have they entered some text ?
    if (keyword == '' || keyword == 'Enter a Keyword'  ) {
        alert('Please enter the text to search');
        if (searchtb!=null) searchtb.focus();
    }
    document.location.href = '/catalog.php?k=' + escape(keyword)
}

   function doClick(e) {
        //the purpose of this function is to allow the enter key to
        //point to the correct button to click.
        var key;

         if(window.event)
              key = window.event.keyCode;     //IE
         else
              key = e.which;     //firefox

        if (key == 13)
        {
            //Get the button the user wants to have clicked
            var btn = document.getElementById('btnSearch');
            if (btn != null)
            { //If we find the button click it
                btn.click();
                event.keyCode = 0
            }
        }
   }
   