var url = "http://www.heritage.sugardog.info";
function $(){   //Prototype's $() function
  var elements = new Array(); 	// array to take elements if there are more than one 
  for (var i = 0; i < arguments.length; i++) { 
    var element = arguments[i];
    if (typeof element == 'string')	//check if it is already an object
	  element = document.getElementById(element);
    if (arguments.length == 1)	
      return element;   // return if there are only one element
    elements.push(element);
  }
  return elements; 		// return multiple objects array
}
//birthdate picker
function getdate(elem){
	id = elem.id.split("_");
	day = $("day_"+id[1]).options[$("day_"+id[1]).selectedIndex].value;
	month = $("month_"+id[1]).options[$("month_"+id[1]).selectedIndex].value;
	year = $("year_"+id[1]).options[$("year_"+id[1]).selectedIndex].value;
	$("date_"+id[1]).value = month+" / "+day+" / "+year;
}
//AutoComplete functions
function Complete(obj, evt) {
  if ((!obj) || (!evt) || (auto.length == 0)){return;}
  if (obj.value.length == 0){ return; }
  var elm = (obj.setSelectionRange) ? evt.which : evt.keyCode;
  if ((elm < 32) || (elm >= 33 && elm <= 46) || (elm >= 112 && elm <= 123)) { return; }
  var txt = obj.value.replace(/;/gi, ",");
  elm = txt.split(",");
  txt = elm.pop();
  txt = txt.replace(/^\s*/, "");

  if (txt.length == 0){return;}
  if (obj.createTextRange) {
   	var rng = document.selection.createRange();
  		if (rng.parentElement() == obj) {
   			elm = rng.text;
  	 		var ini = obj.value.lastIndexOf(elm);
  		}
  } else if (obj.setSelectionRange) {
  		var ini = obj.selectionStart;
  }
  for (var i = 0; i < auto.length; i++) {
   	elm = auto[i].toString();
  		if (elm.toLowerCase().indexOf(txt.toLowerCase()) == 0) {
   			obj.value += elm.substring(txt.length, elm.length);
  	 		break;
  		}
  }
  if (obj.createTextRange) {
  		rng = obj.createTextRange();
  		rng.moveStart("character", ini);
  		rng.moveEnd("character", obj.value.length);
  		rng.select();
  } else if (obj.setSelectionRange) {
  		obj.setSelectionRange(ini, obj.value.length);
  }
}
//Finish Auto complete
function sum(radio,t){
	var val = new Array();
	if(radio.type!='radio'){
		tot1=parseInt($('hid').value);
		if(t){
			tot2=parseInt(price[radio.value])+parseInt(setup[radio.value]);
		}else
			tot2=parseInt(price[radio.value]);
		if(radio.checked)
			tot1=tot1+tot2;
		else 
			tot1=tot1-tot2;
		$('hid').value=tot1;
		$('res').innerHTML='Total : '+tot1;
	}else{
		val = price[radio.value].split(':');
		rval = parseInt(val[0]);
		$('res').innerHTML='Total : '+rval;
	}
}
var tool = "";
function closeDiv(id){
	var id2 = 'div_'+id;
	var currdiv= $(id2);
	var the_divs=document.getElementsByTagName('div');
	var re = RegExp(/^div_/i);
	for(var n=0;n<the_divs.length;n++){
		if(the_divs[n].id.match(re)){
			if (the_divs[n].id==id2 && the_divs[n].style.display=='none'){
				the_divs[n].style.display='block';//Effect.BlindDown(the_divs[n],{duration:0.8});
			}else{
				the_divs[n].style.display='none';//Effect.BlindUp(the_divs[n],{duration:0.8});
			}
		}
	}
	var the_tds=document.getElementsByTagName('td');
	var id3 = 'td_'+id;
	var tdElem = $(id3);
	var re = RegExp(/^td_/i);
	var show = 'url('+url+'/images/splitter_right_show.gif)';
	var hide = 'url('+url+'/images/splitter_right_hide.gif)';
	if(tdElem.style.backgroundImage == show){
		tdElem.style.backgroundImage = hide;
	}else{
		for(var n=0;n<the_tds.length;n++){
			if(the_tds[n].id.match(re)){
				if (the_tds[n].style.backgroundImage == show){
					the_tds[n].style.backgroundImage = hide;
					}
				}
			}
		tdElem.style.backgroundImage = show;
	}
}
function rate(elem,val,hid){
	$(hid).value = val;
}
function resetRate(elem,hid)
{
	var val = $(hid).value;
	if(val == "")
		elem.style.backgroundImage="url("+url+"/images/star0.gif)";
	else
		elem.style.backgroundImage="url("+url+"/images/star"+val+".gif)";
}
function changeColor(val,tab){
	$(tab).style.backgroundImage="url("+url+"/images/star"+val+".gif)";
}
// tooltip code
var offsetfromcursorX=12
var offsetfromcursorY=10
var offsetdivfrompointerX=10
var offsetdivfrompointerY=14
var table = '<div id="tooltip" style="position:absolute;visibility:hidden;z-index: 100;opacity: .7;filter: alpha(opacity=70);">\
<table border="0" cellpadding="0" cellspacing="0">\
  <tr><td height="12"></td>\
    <td width="37" style=" background:url('+url+'/images/tooltip/arrow.gif) 3px no-repeat" ></td>\
    <td></td><td></td>\
  </tr><tr>\
    <td width="12" height="12" background="'+url+'/images/tooltip/topLeft.gif"></td>\
    <td width="37" background="'+url+'/images/tooltip/top.gif"><img src="'+url+'/images/tooltip/arrowBottom.gif"></td>\
    <td background="'+url+'/images/tooltip/top.gif"></td><td width="12"  background="'+url+'/images/tooltip/topRight.gif"></td>\
  </tr><tr>\
  <td height="12" background="'+url+'/images/tooltip/left.gif"></td>\
    <td colspan="2" bgcolor="#D8E0F0"><div id="tooltipinner"></div></td>\
    <td background="'+url+'/images/tooltip/right.gif"></td>\
  </tr><tr>\
    <td height="12" background="'+url+'/images/tooltip/bottomLeft.gif"></td>\
    <td colspan="2" background="'+url+'"/images/tooltip/bottom.gif"></td>\
    <td background="'+url+'/images/tooltip/bottomRight.gif"></td>\
  </tr>\
</table></div>';
document.write(table)
var ie=document.all
var ns6=document.getElementById && !document.all
var enabletip=false
if (ie||ns6)
var tipobj     = document.all? document.all["tooltipinner"] : document.getElementById? $("tooltipinner") : ""
var pointerobj = document.all? document.all["tooltip"]      : document.getElementById? $("tooltip") : ""
function ietruebody(){
	return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}
function ddrivetip(thetext, thewidth, thecolor){
	if(thetext != ""){
		if (ns6||ie){
		if (typeof thewidth!="undefined") tipobj.style.width=thewidth+"px"
		if (typeof thecolor!="undefined" && thecolor!="") tipobj.style.backgroundColor=thecolor
		tipobj.innerHTML=thetext
		enabletip=true
		return false
	}
}}
function positiontip(e){
	if (enabletip){
		var nondefaultpos=false
		var curX=(ns6)?e.pageX : event.clientX+ietruebody().scrollLeft;
		var curY=(ns6)?e.pageY : event.clientY+ietruebody().scrollTop;
		var winwidth=ie&&!window.opera? ietruebody().clientWidth : window.innerWidth-20
		var winheight=ie&&!window.opera? ietruebody().clientHeight : window.innerHeight-20
		var rightedge=ie&&!window.opera? winwidth-event.clientX-offsetfromcursorX : winwidth-e.clientX-offsetfromcursorX
		var bottomedge=ie&&!window.opera? winheight-event.clientY-offsetfromcursorY : winheight-e.clientY-offsetfromcursorY
		var leftedge=(offsetfromcursorX<0)? offsetfromcursorX*(-1) : -1000
		if (rightedge<tipobj.offsetWidth){
			tipobj.style.left=curX-tipobj.offsetWidth+"px"
		nondefaultpos=true
	}
	else if (curX<leftedge)
		tipobj.style.left="5px"
	else{
		tipobj.style.left=(curX+offsetfromcursorX-offsetdivfrompointerX-25)+"px" //position the horizontal position of the menu where the mouse is positioned
		pointerobj.style.left=(curX+offsetfromcursorX-25)+"px"
	}
	if (bottomedge<tipobj.offsetHeight){
		tipobj.style.top=(curY-tipobj.offsetHeight-offsetfromcursorY+15)+"px"
		nondefaultpos=true
	}
	else{
		tipobj.style.top=curY+offsetfromcursorY+offsetdivfrompointerY+"px"
		pointerobj.style.top=curY+offsetfromcursorY+"px"
	}
	tipobj.style.visibility="visible"
	if (!nondefaultpos)
		pointerobj.style.visibility="visible"
	else
		pointerobj.style.visibility="hidden"
	}
}
function hideddrivetip(){
	if (ns6||ie){
		enabletip=false
		tipobj.style.visibility="hidden"
		pointerobj.style.visibility="hidden"
		tipobj.style.left="-1000px"
		tipobj.style.backgroundColor=''
		tipobj.style.width=''
	}
}
document.onmouseover=positiontip

var errored = false; // Global for validate function
var exClassName = new Object(); // Global for Changed classnames
function validate(elem,type){	// Main function

	///////////
	//Checking for mail validation
	var checkmail = function(email){
		var splitted = email.match("^(.+)@(.+)$");
			if(splitted == null) return false;
			if(splitted[1] != null )   {
				var regexp_user=/^\"?[\w-_\.]*\"?$/;
				if(splitted[1].match(regexp_user) == null) return false;  
			}
			if(splitted[2] != null)  {
				var regexp_domain=/^[\w-\.]*\.[A-Za-z]{2,4}$/;
				if(splitted[2].match(regexp_domain) == null) {
					var regexp_ip =/^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/;
					if(splitted[2].match(regexp_ip) == null) return false; 
				}
				return true; 
			}
		return false;
	}
	
	//////////////
	// Function to run onsubmit;
	var onSubmit = function(elem){
		errored = false;
		var form = document.forms[elem];
		for(var node = 0;node < form.length; node++)
			if((""+form[node].onblur).match("validate"))
				form[node].onblur();
		if(errored)	return false; 
		else form.submit();
	}
	
	//////////////
	// Function to set error messages
	var getMsg = function(type){
		switch(type){
			case "Alphabetic": return "Cannot contain non-alphabetic characters."
			case "RequiredAlphabetic": return "This field is required and cannot contain non-alphabetic characters."
			case "Numeric": return "Cannot contain non-numeric characters."
			case "RequiredNumeric": return "This field is required and cannot contain non-numeric characters."
			case "Email": return "Enter a valid email address."
			case "Required": return "This field is required."
			default: return "Undefined Error Message"
		}
	}
	
	/////////////
	// Function to check is error printed?
	var checkForErrorDiv = function(elem){
		for(var node = 0;node < elem.parentNode.childNodes.length; node++)
			if(elem.parentNode.childNodes[node].className == "Errortext")
				return true;
		return false;
	}
	
	/////////////
	// Function to define errors and print messages
	var error = function(elem,type){
		if(!checkForErrorDiv(elem)){
			div = document.createElement("div");
			div.innerHTML = getMsg(type);
			div.className = "Errortext";
			elem.parentNode.insertBefore(div, elem.nextSibling);
		}
		if(elem.className != "error")
			exClassName[elem] = elem.className;
		elem.className = "error";
		errored = true;
		return true;
	}
	
	/////////////
	// Function to revert Errored fields
	var correct = function(elem){
		if(elem.nextSibling)
			elem.parentNode.removeChild(elem.nextSibling);
		if(elem.className == "error")
			elem.className = exClassName[elem];
	}
	
	//////////////
	// Make all the validations
	switch(type){
		case "Alphabetic":
			var charpos = elem.value.search(/[^a-zA-Z\s\-\_]/);
			if(charpos >= 0) error(elem,type); else correct(elem);
			break;
		case "RequiredAlphabetic":
			if(elem.value.length <= 0) error(elem,type); 
			else{
				var charpos = elem.value.search(/[^a-zA-Z\s\-\_]/);
				if(charpos >= 0) error(elem,type); else correct(elem);
			}
			break;
		case "Numeric":
			var charpos = elem.value.search(/[^0-9\.\,\s\-\_]/);
			if(charpos >= 0) error(elem,type); else correct(elem);
			break;
		case "RequiredNumeric":
			if(elem.value.length <= 0) error(elem,type); 
			else{
				var charpos = elem.value.search(/[^0-9\.\,\s\-\_]/);
				if(charpos >= 0) error(elem,type); else correct(elem);
			}
			break;
		case "Email":
			if(!checkmail(elem.value)) error(elem,type); else correct(elem);
			break;
		case "Required":
			if(elem.value.length <= 0) error(elem,type); else correct(elem);
			break;
		default:	// Default is for defining the form and setting onsubmit function
			var form = document.forms[elem]
			form.onsubmit = function(){ onSubmit(elem); return false; };
			break;
	}		
}

