﻿var ua = navigator.userAgent;
var dom = (document.getElementById) ? true : false;
var ie4 = (document.all && !dom) ? true : false;
var ie5_5 = ((ua.indexOf("MSIE 5.5")>=0 || ua.indexOf("MSIE 6")>=0) && ua.indexOf("Opera")<0) ? true : false;
var ns4 = (document.layers && !dom) ? true : false;
var newwindow;
var diffWidth=0; diffHeight=0;

function CreateXmlReq()
{
    var objXmlReq;
	try{
		objXmlReq = new ActiveXObject("Msxml2.XMLHTTP");
	}catch(e){
		try{
			objXmlReq = new ActiveXObject("Microsoft.XMLHTTP");
		}catch(oc){
			objXmlReq = null;
		}
	}
	if(!objXmlReq && typeof XMLHttpRequest != "undefined"){
		objXmlReq = new XMLHttpRequest();
	}
	return objXmlReq;
}

function GetLookBookItems(Comp){
	var XmlReq = CreateXmlReq();
    var requestUrl = "GetLookBookItems.aspx?RndId=" + Math.random();    
    if(XmlReq)
	{	
		XmlReq.onreadystatechange = function(){
			BuildLookBook(XmlReq,Comp);
		};		
		XmlReq.open("POST", requestUrl);
		XmlReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		XmlReq.send("Language=EN&InnoRand=" + Math.random());	
	}
}

function BuildLookBook(XmlReq,Comp){
	if(XmlReq.readyState == 4){
		if(XmlReq.status == 200){			
			document.getElementById(Comp).innerHTML = XmlReq.responseText;
		}else{
			document.getElementById(Comp).innerHTML = "<TABLE height='400' width='100%'><TR><TD class='adminblack' align='center' valign='middle'><table><tr><td class='adminblack' align='center'>" + XmlReq.statusText + "...</td></tr></table></TD></TR></TABLE>";
		}
	}
}

function winopen(imgurl,width,height,filename,id,Lang,LookBookId){	
	if(newwindow!=null){
		if(!newwindow.closed){
			newwindow.close();
		}
	}
	//var url = "LookBookImage.htm?url=" + imgurl + "&filename=" + filename + "&id=" + id + "&Lang=" + Lang + "&LookBookId=" + LookBookId;
	var url = "LookBookInspirational.aspx?url=" + imgurl + "&filename=" + filename + "&id=" + id + "&Lang=" + Lang + "&LookBookId=" + LookBookId;
	var windowWidth;
    var windowHeight;
	windowHeight = height;
	windowWidth = width;
	var xcen = 100;
	var ycen = 100;
	if (window.screen){
      var avht = screen.availHeight;
      var avwd = screen.availWidth;
      xcen = (avwd - windowWidth) / 2;
      ycen = (avht - windowHeight) / 2;
    }
    var params = "left="+ xcen +",top="+ ycen +",width="+ windowWidth +",height="+ windowHeight +",status=yes,menubar=no,resizable=no,scrollbars=no";
	//newwindow = window.open(url,"Stonemark",params);
	window.location = url;
	return false;
}

function showImage(imgurl,width,height,filename,id,Lang,LookBookId){		
	//var url = "LookBookImage.htm?url=" + imgurl + "&filename=" + filename + "&id=" + id + "&Lang=" + Lang + "&LookBookId=" + LookBookId;
	var url = "LookBookInspirational.aspx?url=" + imgurl + "&filename=" + filename + "&id=" + id + "&Lang=" + Lang + "&LookBookId=" + LookBookId;
	//window.location.replace(url);
	window.location = url;
}

function submitfrm(){
	document.forms[0].submit();
}

function QueryString(name){  
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");  
	var regexS = "[\\?&]"+name+"=([^&#]*)";  
	var regex = new RegExp( regexS );  
	var results = regex.exec( window.location.href );  
	if(results == null){
		return "";
	}else{
	    return results[1];
	}
}

function GetInnerWidth(){
   var WinInnerWid = 630;  
  if( typeof( window.innerWidth ) == 'number' ) {    
    WinInnerWid = window.innerWidth;    
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {    
    WinInnerWid = document.documentElement.clientWidth;    
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {    
    WinInnerWid = document.body.clientWidth;    
  }  
  return WinInnerWid;   
}

function GetInnerHeight(){
  var WinInnerHei = 460;  
  if( typeof( window.innerWidth ) == 'number' ) {    
    WinInnerHei = window.innerHeight;    
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {    
    WinInnerHei = document.documentElement.clientHeight;    
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {    
    WinInnerHei = document.body.clientHeight;    
  }  
  return WinInnerHei;
}

var Width = 0; Height = 0; ran = 0; resizeRate = 6; //Resize rate per minute
var ObjId = "";
function SetWindowDimNCenter(BoundObjId){
    resizeRate = Math.round(60000/resizeRate);    
    ObjId = BoundObjId;
    if(document.getElementById(ObjId)){
        SetDifference();
        SetWidthHeight(BoundObjId);
        windowWidth = Width + diffWidth;
        windowHeight = Height + diffHeight;
        if (window.screen){
            window.moveTo((screen.availWidth - windowWidth) / 2,(screen.availHeight - windowHeight) / 2);
        }
        keepWindowSize();
    }
}
var KeepWindowInt;
function keepWindowSize(){
   if(document.getElementById(ObjId)){
       if(ran!=0){    
        SetWidthHeight(ObjId);
        windowWidth = parseFloat(Width) + parseFloat(diffWidth);
        windowHeight = parseFloat(Height) + parseFloat(diffHeight);
       }
       ran++;
       //document.title = ran + " times runned....";   
       window.resizeTo(windowWidth,windowHeight);
       //window.defaultStatus = "Window is resized with " + windowWidth + "px Width and " + windowHeight + "px Height";
       clearInterval(KeepWindowInt);
       KeepWindowInt = setTimeout("keepWindowSize();",resizeRate);
   }else{
       clearInterval(KeepWindowInt);
   }   
}

var checkParentPresentOnce = 50; // milliseconds
function closeMeWhenParentClosed(){
    if(window.opener.closed){
        window.close();
    }
    setTimeout("closeMeWhenParentClosed();",checkParentPresentOnce);
}

function SetWidthHeight(ObjName){
    var Obj = document.getElementById(ObjName);
    if(Obj!=null){
        Width = Obj.offsetWidth;
        Height = Obj.offsetHeight;        
    }else{
        window.defaultStatus = ObjName + " was not found..";
    }
}

function SetDifference(){
    window.resizeTo(500,500);
    diffWidth = 500 - GetInnerWidth();
    diffHeight = 500 - GetInnerHeight();    
}

function closewindow(){
    window.close();
}

onerror = function(err,url,l){
  window.defaultStatus = err + " on line " + l;
  if(KeepWindowInt && ObjId){
    clearInterval(KeepWindowInt);
    KeepWindowInt = setTimeout("keepWindowSize();",resizeRate);
  }
  return true;
}