// JavaScript Document

// Image Rollover Pre-Loader
// Example: MM_preloadImages('on_image_filename1','on_image_filename2','on_image_filename3');
function MM_preloadImages() {
	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];
			}
	}
}

// Image Rollover 
// Example: onmouseout="MM_swapImgRestore();" 
// Example:	onmouseover="MM_swapImage('image_field_name','','on_image_filename',1);"
function MM_findObj(n, d) { 
	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() { 
	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 MM_swapImgRestore() { 
	var i,x,a=document.MM_sr; 
	for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) 
  		x.src=x.oSrc;
}

// Email Address Validation
function echeck(str) {
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1)
		return false;
	
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
		return false;

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
		return false;

	if (str.indexOf(at,(lat+1))!=-1)
		return false;

	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
		return false;

	if (str.indexOf(dot,(lat+2))==-1)
		return false;
		
	if (str.indexOf(" ")!=-1)
		return false;
	
	if (str.indexOf(",")!=-1)
		return false;

return true					
}

// Integer Check
function isInteger(s)
{   
	var i;
    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    return true;
}

// Currency Check
function isMoney(s){
	var i;
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (((c < "0") || (c > "9")) && ((c != ".") && (c != "," )))
			return false;
		if (c == ".")
			if (s.length > (i + 3))
				return false;
    }
    return true;
}

//open new window script for XHTML 1.0 Strict
function externalLinks() {
	if (!document.getElementsByTagName) 
		return; 
	var anchors = document.getElementsByTagName("a"); 
 	for (var i=0; i<anchors.length; i++) { 
   		var anchor = anchors[i]; 
   		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") 
     		anchor.target = "_blank"; 
 	} 
} 

window.onload = externalLinks;


// Ties and Cuffs website
function check_qty(obj,old_value,max_value) {
	if (!isInteger(obj.value) || (obj.value == 0))
	{
		alert("please enter a valid number for quantity");
		obj.value = old_value;
		obj.focus();
		return false;
	}
	if (parseInt(obj.value) > parseInt(max_value)) {
		alert("Quantity not available.\nPlease contact us at info@tiesncuffs.com.au\nto see if we can organise");
		obj.value = old_value;
		obj.focus();
		return false;
	}
	
	return true;
}

function check_qty_new(obj,old_value,max_value) {
	if (isInteger(obj.value))
	{
		if ((parseInt(obj.value) > parseInt(max_value))) {
			alert("Quantity not available.\nPlease contact us at info@tiesncuffs.com.au\nto see if we can organise");
			obj.value = old_value;
			obj.focus();
			return false;
		}
	}
	
	return true;
}

function noenter() {
  return !(window.event && window.event.keyCode == 13); 
}

function check_user_login() {
	if (document.getElementById('user_login').username.value == "")
	{
		alert("please enter your username");
		document.getElementById('user_login').username.focus();
		return false;
	}
	
	if (document.getElementById('user_login').password.value == "")
	{
		alert("please enter your password");
		document.getElementById('user_login').password.focus();
		return false;
	}
	
	return true;
}

function check_user_password() {
	if (document.getElementById('user_login').username.value == "")
	{
		alert("please enter your username");
		document.getElementById('user_login').username.focus();
		return false;
	}
	
	return true;
}

function check_tie_admin() {
	if (document.getElementById('tie_frm').tie_id.value == 0)
	{
		alert ("Please choose a tie for editing");
		document.getElementById('tie_frm').tie_id.focus();
		return false;
	}
	
	return true;
}

function check_access_admin() {
	if (document.getElementById('access_frm').access_id.value == 0)
	{
		alert ("Please choose an accessory for editing");
		document.getElementById('access_frm').access_id.focus();
		return false;
	}
	
	return true;
}

function set_shipping() {
	if (document.getElementById('user_detail').ship_same.checked == true)
	{
		document.getElementById('user_detail').shipping_fname.value = document.getElementById('user_detail').billing_fname.value;
		document.getElementById('user_detail').shipping_lname.value = document.getElementById('user_detail').billing_lname.value;
		document.getElementById('user_detail').shipping_address1.value = document.getElementById('user_detail').billing_address1.value;
		document.getElementById('user_detail').shipping_address2.value = document.getElementById('user_detail').billing_address2.value;
		document.getElementById('user_detail').shipping_suburb.value = document.getElementById('user_detail').billing_suburb.value;
		document.getElementById('user_detail').shipping_state.value = document.getElementById('user_detail').billing_state.value;
		document.getElementById('user_detail').shipping_postcode.value = document.getElementById('user_detail').billing_postcode.value;
		document.getElementById('user_detail').shipping_country.value = document.getElementById('user_detail').billing_country.value;
	}
}

function check_user_detail() {
	if (document.getElementById('user_detail').username.value == "")
	{
		alert("Please enter your username / email address");
		document.getElementById('user_detail').username.focus();
		return false;
	}
	
	if (!echeck(document.getElementById('user_detail').username.value))
	{
		alert("Please enter a valid email address as your username");
		document.getElementById('user_detail').username.focus();
		return false;
	}
	
	if (document.getElementById('user_detail').password1.value == "")
	{
		alert("Please enter your password");
		document.getElementById('user_detail').password1.focus();
		return false;
	}
	
	if (document.getElementById('user_detail').password2.value == "")
	{
		alert("Please repeat your password");
		document.getElementById('user_detail').password2.focus();
		return false;
	}
	
	if (document.getElementById('user_detail').password1.value != document.getElementById('user_detail').password2.value)
	{
		alert("two passwords not match, please re-enter your password");
		document.getElementById('user_detail').password1.focus();
		return false;
	}
	
	if (document.getElementById('user_detail').billing_fname.value == "")
	{
		alert("Please enter your first name for billing address");
		document.getElementById('user_detail').billing_fname.focus();
		return false;
	}
	
	if (document.getElementById('user_detail').billing_lname.value == "")
	{
		alert("Please enter your last name for billing address");
		document.getElementById('user_detail').billing_lname.focus();
		return false;
	}
	
	if (document.getElementById('user_detail').billing_address1.value == "")
	{
		alert("Please enter your billing address");
		document.getElementById('user_detail').billing_address1.focus();
		return false;
	}
	
	if (document.getElementById('user_detail').billing_suburb.value == "")
	{
		alert("Please enter the suburb your billing address");
		document.getElementById('user_detail').billing_suburb.focus();
		return false;
	}
	
	if (document.getElementById('user_detail').billing_state.value == "")
	{
		alert("Please enter the state your billing address");
		document.getElementById('user_detail').billing_state.focus();
		return false;
	}
	
	if (document.getElementById('user_detail').billing_postcode.value == "")
	{
		alert("Please enter the postcode your billing address");
		document.getElementById('user_detail').billing_postcode.focus();
		return false;
	}
	
	if (document.getElementById('user_detail').billing_phone.value == "")
	{
		alert("Please enter the your phone number");
		document.getElementById('user_detail').billing_phone.focus();
		return false;
	}
	
	if (document.getElementById('user_detail').shipping_fname.value == "")
	{
		alert("Please enter your first name for shipping address");
		document.getElementById('user_detail').shipping_fname.focus();
		return false;
	}
	
	if (document.getElementById('user_detail').shipping_lname.value == "")
	{
		alert("Please enter your last name for shipping address");
		document.getElementById('user_detail').shipping_lname.focus();
		return false;
	}
	
	if (document.getElementById('user_detail').shipping_address1.value == "")
	{
		alert("Please enter your shipping address");
		document.getElementById('user_detail').shipping_address1.focus();
		return false;
	}
	
	if (document.getElementById('user_detail').shipping_suburb.value == "")
	{
		alert("Please enter the suburb your shipping address");
		document.getElementById('user_detail').shipping_suburb.focus();
		return false;
	}
	
	if (document.getElementById('user_detail').shipping_state.value == "")
	{
		alert("Please enter the state your shipping address");
		document.getElementById('user_detail').shipping_state.focus();
		return false;
	}
	
	if (document.getElementById('user_detail').shipping_postcode.value == "")
	{
		alert("Please enter the postcode your shipping address");
		document.getElementById('user_detail').shipping_postcode.focus();
		return false;
	}
	
	return true;
}

function check_tie_edit() {
	if (document.getElementById('tie_frm').product_code.value == "")
	{
		alert("please enter the product code");
		document.getElementById('tie_frm').product_code.focus();
		return false;
	}
	
	if (document.getElementById('tie_frm').product_name.value == "")
	{
		alert("please enter the product name");
		document.getElementById('tie_frm').product_name.focus();
		return false;
	}
	
	if (document.getElementById('tie_frm').colour.value == "")
	{
		alert("please enter the product colour");
		document.getElementById('tie_frm').colour.focus();
		return false;
	}
	
	if (document.getElementById('tie_frm').description.value == "")
	{
		alert("please enter the product description");
		document.getElementById('tie_frm').description.focus();
		return false;
	}
	
	if (document.getElementById('tie_frm').stock_level.value == "")
	{
		alert("please enter the stock level");
		document.getElementById('tie_frm').stock_level.focus();
		return false;
	}
	
	if (!isInteger(document.getElementById('tie_frm').stock_level.value))
	{
		alert("please enter the a valid stock level");
		document.getElementById('tie_frm').stock_level.focus();
		return false;
	}
	
	if (document.getElementById('tie_frm').price.value == "")
	{
		alert("please enter the price");
		document.getElementById('tie_frm').price.focus();
		return false;
	}
	
	if (!isMoney(document.getElementById('tie_frm').price.value))
	{
		alert("please enter the a valid price");
		document.getElementById('tie_frm').price.focus();
		return false;
	}
	
	if ((document.getElementById('tie_frm').recommend_1.value == 0) &&
		(document.getElementById('tie_frm').recommend_2.value == 0) &&
		(document.getElementById('tie_frm').recommend_3.value == 0) &&
		(document.getElementById('tie_frm').recommend_4.value == 0) &&
		(document.getElementById('tie_frm').recommend_5.value == 0))
	{
		alert("please choose at least one recommend pair");
		document.getElementById('tie_frm').recommend_1.focus();
		return false;
	}
	
	return true;
}

function check_access_edit() {
	if (document.getElementById('access_frm').product_code.value == "")
	{
		alert("please enter the product code");
		document.getElementById('access_frm').product_code.focus();
		return false;
	}
	
	if (document.getElementById('access_frm').product_name.value == "")
	{
		alert("please enter the product name");
		document.getElementById('access_frm').product_name.focus();
		return false;
	}
	
	if (document.getElementById('access_frm').colour.value == "")
	{
		alert("please enter the product colour");
		document.getElementById('access_frm').colour.focus();
		return false;
	}
	
	if (document.getElementById('access_frm').description.value == "")
	{
		alert("please enter the product description");
		document.getElementById('access_frm').description.focus();
		return false;
	}
	
	if (document.getElementById('access_frm').stock_level.value == "")
	{
		alert("please enter the stock level");
		document.getElementById('access_frm').stock_level.focus();
		return false;
	}
	
	if (!isInteger(document.getElementById('access_frm').stock_level.value))
	{
		alert("please enter the a valid stock level");
		document.getElementById('access_frm').stock_level.focus();
		return false;
	}
	
	if (document.getElementById('access_frm').price.value == "")
	{
		alert("please enter the price");
		document.getElementById('access_frm').price.focus();
		return false;
	}
	
	if (!isMoney(document.getElementById('access_frm').price.value))
	{
		alert("please enter the a valid price");
		document.getElementById('access_frm').price.focus();
		return false;
	}
	
	return true;
}

function check_newsletter() {
	
	if (document.newsletter_frm.newsletter_email.value == "")
	{
		alert("Please enter your email address");
		document.newsletter_frm.newsletter_email.focus();
		return false;
	}
	
	if (!echeck(document.newsletter_frm.newsletter_email.value))
	{
		alert("Please enter a valid email address");
		document.newsletter_frm.newsletter_email.focus();
		return false;
	}

	return true;
}

function check_unsubscribe() {
	
	if (document.getElementById('tie_frm').newsletter_email.value == "")
	{
		alert("Please enter the unsubscribe email address");
		document.getElementById('tie_frm').newsletter_email.focus();
		return false;
	}
	
	if (!echeck(document.getElementById('tie_frm').newsletter_email.value))
	{
		alert("Please enter a valid email address");
		document.getElementById('tie_frm').newsletter_email.focus();
		return false;
	}

	return true;
}

function addbookmark()
{
	bookmarkurl="http://www.tiesncuffs.com.au/"
	bookmarktitle="Ties and Cuffs"
	if (document.all)
		window.external.AddFavorite(bookmarkurl,bookmarktitle)
	else
		window.sidebar.addPanel(bookmarktitle, bookmarkurl, "")
}

function check_emailfriend() {
	
	if (document.emailfriend_frm.your_name.value == "")
	{
		alert("Please enter your name");
		document.emailfriend_frm.your_name.focus();
		return false;
	}
	
	if (document.emailfriend_frm.your_email.value == "")
	{
		alert("Please enter your email address");
		document.emailfriend_frm.your_email.focus();
		return false;
	}
	
	if (!echeck(document.emailfriend_frm.your_email.value))
	{
		alert("Please enter your valid email address");
		document.emailfriend_frm.your_email.focus();
		return false;
	}
	
	if (document.emailfriend_frm.friend_name.value == "")
	{
		alert("Please enter your friend's name");
		document.emailfriend_frm.friend_name.focus();
		return false;
	}
	
	if (document.emailfriend_frm.friend_email.value == "")
	{
		alert("Please enter your friend's email address");
		document.emailfriend_frm.friend_email.focus();
		return false;
	}
	
	if (!echeck(document.emailfriend_frm.friend_email.value))
	{
		alert("Please enter a valid email address for your friend");
		document.emailfriend_frm.friend_email.focus();
		return false;
	}

	return true;
}

function searchbycolor(){
if (document.getElementById('search_frm').col_id.value != "")
window.document.location.replace('products.asp?col_id=' + document.getElementById('search_frm').col_id.value);
}

function searchbyprice(){
if (document.getElementById('search_frm').p_id.value != "")
window.document.location.replace('products.asp?p_id=' + document.getElementById('search_frm').p_id.value);
}

function searchbycategory(){
if (document.getElementById('search_frm').c_id.value != "")
window.document.location.replace(document.getElementById('search_frm').c_id.value);
}

function searchbycolor2(){
if (document.getElementById('search_frm2').col_id.value != "")
window.document.location.replace('products.asp?col_id=' + document.getElementById('search_frm2').col_id.value);
}

function searchbyprice2(){
if (document.getElementById('search_frm2').p_id.value != "")
window.document.location.replace('products.asp?p_id=' + document.getElementById('search_frm2').p_id.value);
}

function searchbycolor3(){
if (document.getElementById('search_frm3').c_id.value != "")
window.document.location.replace('products.asp?c_id=' + document.getElementById('search_frm3').c_id.value);
}

function searchbyprice3(){
if (document.getElementById('search_frm3').p_id.value != "")
window.document.location.replace('products.asp?p_id=' + document.getElementById('search_frm3').p_id.value);
}

function searchbycolor4(){
if (document.getElementById('search_frm4').c_id.value != "")
window.document.location.replace('products.asp?c_id=' + document.getElementById('search_frm4').c_id.value);
}

function searchbyprice4(){
if (document.getElementById('search_frm4').p_id.value != "")
window.document.location.replace('products.asp?p_id=' + document.getElementById('search_frm4').p_id.value);
}

function getImageWidth(myImage) {
	var x, obj;
	if (document.layers) {
		var img = getImage(myImage);
		return img.width;
	} else {
		return getElementWidth(myImage);
	}
	return -1;
}

function getImageHeight(myImage) {
	var y, obj;
	if (document.layers) {
		var img = getImage(myImage);
		return img.height;
	} else {
		return getElementHeight(myImage);
	}
	return -1;
}


